Created
December 14, 2015 19:21
-
-
Save calendee/9d3928c9c9fc0303a77d to your computer and use it in GitHub Desktop.
Count AngularJS Watches
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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