Skip to content

Instantly share code, notes, and snippets.

@egm0121
Created March 10, 2017 13:14
Show Gist options
  • Save egm0121/f2f1a190567aa6fe9798c82ab393b746 to your computer and use it in GitHub Desktop.
Save egm0121/f2f1a190567aa6fe9798c82ab393b746 to your computer and use it in GitHub Desktop.
angular.js Dom based active watchers counter
function getWatchers(root) {
root = angular.element(root || document.documentElement);
var watcherCount = 0;
var scopeArr = [];
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 && scopeArr.indexOf(scope.$id) == -1) {
scopeArr.push(scope.$id)
return scope.$$watchers || [];
} else {
return [];
}
}
return getElemWatchers(root);
}
//usage:
//getWatchers(document.body).length
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment