Skip to content

Instantly share code, notes, and snippets.

@DorkForce
Last active July 25, 2016 17:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DorkForce/a2eba27cb12abbf56cd6 to your computer and use it in GitHub Desktop.
Save DorkForce/a2eba27cb12abbf56cd6 to your computer and use it in GitHub Desktop.
Console Script to Count Angular Watchers - change 'body' to whichever element has ng-app
(function () {
var root = angular.element(document.getElementsByTagName('body'));
var watchers = [];
var f = function (element) {
angular.forEach(['$scope', '$isolateScope'], function (scopeProperty) {
if (element.data() && element.data().hasOwnProperty(scopeProperty)) {
angular.forEach(element.data()[scopeProperty].$$watchers, function (watcher) {
watchers.push(watcher);
});
}
});
angular.forEach(element.children(), function (childElement) {
f(angular.element(childElement));
});
};
f(root);
// Remove duplicate watchers
var watchersWithoutDuplicates = [];
angular.forEach(watchers, function(item) {
if(watchersWithoutDuplicates.indexOf(item) < 0) {
watchersWithoutDuplicates.push(item);
}
});
console.log(watchersWithoutDuplicates.length);
})();
@DorkForce
Copy link
Author

Found via http://stackoverflow.com/questions/18499909/how-to-count-total-number-of-watches-on-a-page ... Load up an angular page and paste this function into the console. After a moment this will return the number of active watchers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment