Created
February 18, 2015 17:02
-
-
Save rkoch/899864b2d0970721307a to your computer and use it in GitHub Desktop.
count angular watchers snippet for chrome
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 countWatchers (root) { | |
root = angular.element(root || document.documentElement); | |
var scopes = {}; | |
function traverseElement (element) { | |
var count = 0; | |
var data = element.data(); | |
count += countWatchersFromScope(data.$isolateScope); | |
count += countWatchersFromScope(data.$scope); | |
angular.forEach(element.children(), function (childElement) { | |
count += traverseElement(angular.element(childElement)); | |
}); | |
return count; | |
} | |
function countWatchersFromScope(scope) { | |
if (scope) { | |
if (!scopes.hasOwnProperty(scope.$id)) { | |
scopes[scope.$id] = true; | |
return scope.$$watchers ? scope.$$watchers.length : 0; | |
} | |
} | |
return 0; | |
} | |
return traverseElement(root); | |
} | |
countWatchers(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment