Skip to content

Instantly share code, notes, and snippets.

@calendee
Created December 14, 2015 19:21
Show Gist options
  • Save calendee/9d3928c9c9fc0303a77d to your computer and use it in GitHub Desktop.
Save calendee/9d3928c9c9fc0303a77d to your computer and use it in GitHub Desktop.
Count AngularJS Watches
(function countAngularWatchers(angular) {
var i, data, scope,
count = 0,
all = document.all,
len = all.length,
test = {};
var mostWatchers = 0;
function countScopeWatchers(scope, element) {
test[scope.$id] = true;
var n = scope.$$watchers.length;
count += n;
if (n > mostWatchers) {
console.log('most watchers', n);
console.log(element);
mostWatchers = n;
}
}
// go through each element. Count watchers if it has scope or isolate scope
for (i = 0; i < len; i += 1) {
var el = angular.element(all[i]);
data = el.data();
scope = data.$scope || data.$isolateScope;
if (scope && scope.$$watchers) {
if ( !test[ scope.$id ] ) {
countScopeWatchers(scope, el);
}
}
}
console.log('this page has', count, 'angular watchers');
return count;
}(window.angular));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment