Javascript 一页有多少块手表?

Javascript 一页有多少块手表?,javascript,performance,angularjs,watch,Javascript,Performance,Angularjs,Watch,我正在Angular进行性能测试,我想知道我的页面中到底有多少手表。事实证明,要做到这一点并不容易。有人试过吗 任何帮助都将不胜感激 我也有同样的问题。我创建了一个函数来实现这一点: // get the watch count // scopeHash is an optional parameter, but if you provide one, this function will modify it for later use (possibly debugging) function

我正在Angular进行性能测试,我想知道我的页面中到底有多少手表。事实证明,要做到这一点并不容易。有人试过吗


任何帮助都将不胜感激

我也有同样的问题。我创建了一个函数来实现这一点:

// get the watch count
// scopeHash is an optional parameter, but if you provide one, this function will modify it for later use (possibly debugging)
function getWatchCount (scope, scopeHash) {
    // default for scopeHash
    if (scopeHash === undefined) {
        scopeHash = {};
    }

    // make sure scope is defined and we haven't already processed this scope
    if (!scope || scopeHash[scope.$id] !== undefined) {
        return 0;
    }

    var watchCount = 0;

    if (scope.$$watchers) {
        watchCount = scope.$$watchers.length;
    }
    scopeHash[scope.$id] = watchCount;

    // get the counts of children and sibling scopes
    // we only need childHead and nextSibling (not childTail or prevSibling)
    watchCount+= getWatchCount(scope.$$childHead, scopeHash);
    watchCount+= getWatchCount(scope.$$nextSibling, scopeHash);

    return watchCount;
}

它将计算任何作用域上的观察者数量。在根作用域上进行计算可能是最有用的,但您可以在任何作用域级别使用它(可能用于检查组件上的监视)。下面是一个实例:

快速脏解决方案的想法:注册全局计数器,并在负责注册/注销手表的方法中递增/递减它。当然-您必须编辑Angular.js代码。您可以在本地包装Angular。$watch并使用本地版本,该版本可以记录或推送到您需要的任何地方。检查
$scope.watchers
是的,本地包装甚至比直接编辑Angular的代码更好(如@dandavis所建议)@dandavis你能给我举个例子说明怎么做吗?这包括隔离示波器吗?如果没有,如何将其包括在内?