Last active
April 4, 2016 12:58
-
-
Save TiddoLangerak/c2c2fff4dc12a292995833bc734133d0 to your computer and use it in GitHub Desktop.
Functions to get the number of watchers for elements
This file contains hidden or 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
//getWatchers function from: https://medium.com/@kentcdodds/counting-angularjs-watchers-11c5134dc2ef#.uh88feqpr | |
function getWatchers(root) { | |
root = angular.element(root || document.documentElement); | |
var watcherCount = 0; | |
function getElemWatchers(element) { | |
var isolateWatchers = getWatchersFromScope(element.data().$isolateScope); | |
var scopeWatchers = getWatchersFromScope(element.data().$scope); | |
var watchers = scopeWatchers.concat(isolateWatchers); | |
angular.forEach(element.children(), function (childElement) { | |
watchers = watchers.concat(getElemWatchers(angular.element(childElement))); | |
}); | |
return watchers; | |
} | |
function getWatchersFromScope(scope) { | |
if (scope) { | |
return scope.$$watchers || []; | |
} else { | |
return []; | |
} | |
} | |
return getElemWatchers(root); | |
} | |
function getWatcherTree(root) { | |
var watchers = getWatchers(root).length; | |
var tree = { | |
el : root, | |
watchers: watchers, | |
children : [], | |
}; | |
if (watchers > 0) { | |
tree.children = [].slice.apply(root.children).map(getWatcherTree).sort(function(a, b) { return b.watchers - a.watchers }); | |
} | |
return tree; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment