Skip to content

Instantly share code, notes, and snippets.

@AsifulNobel
Last active June 4, 2020 12:56
Show Gist options
  • Save AsifulNobel/d766cd5c75865324db40714bf91b1e0a to your computer and use it in GitHub Desktop.
Save AsifulNobel/d766cd5c75865324db40714bf91b1e0a to your computer and use it in GitHub Desktop.
AngularJS watcher script to get count of all watchers in the current scope and nested scopes.
function getWatcherCount(root) {
root = angular.element(root || document.documentElement);
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).length;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment