Skip to content

Instantly share code, notes, and snippets.

@psdcoder
Created July 15, 2015 10:59
Show Gist options
  • Select an option

  • Save psdcoder/75dc2c283fab6e4a786b to your computer and use it in GitHub Desktop.

Select an option

Save psdcoder/75dc2c283fab6e4a786b to your computer and use it in GitHub Desktop.
Get count of angularjs watchers
//from https://medium.com/@kentcdodds/counting-angularjs-watchers-11c5134dc2ef
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);
}
getWatchers().length
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment