javascript - 一个页面中有多少个 watch ?

标签 javascript performance angularjs watch

我正在 Angular 中进行性能测试,我想确切地知道我的页面中有多少个 watch 。事实证明,没有简单的方法可以做到这一点。有人尝试过吗?

任何帮助将不胜感激!

最佳答案

我也有同样的问题。我创建了一个可以执行此操作的函数:

// 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;
}

它将计算任何范围内的观察者数量。在根作用域上进行计算可能最有用,但您可以在任何作用域级别使用它(可能是为了检查组件上的监视)。这是一个实际示例:http://jsfiddle.net/S7APg/

关于javascript - 一个页面中有多少个 watch ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18704503/

相关文章:

angularjs - 在 ng-init 中添加过滤器

javascript - 如何验证输入到 3 个单独字段中的日期?

JavaScript - 通过 DOM 遍历获取 href 属性值

javascript - 如何将底部 div 保持在顶部 div 下方的恒定高度,并且它们之间有一个 div?

performance - Html-Webpack-Plugin 在多个 pug 文件上非常慢

具有流和性能的 Java 8 嵌套循环

c# - 在页面加载时隐藏 div 标签

javascript - 在使用 Chrome 进行分析时,React 的性能显着提高

javascript - 如何将数据从自定义 Angular 元素传递到 Controller ?

javascript - 如何使用 anchor 标记与 $stateProvider 重定向到 Angular js 中的另一个页面?