Skip to content

Instantly share code, notes, and snippets.

@jlandure
Forked from taesup/angular-count-watchers
Created September 22, 2015 15:02
Show Gist options
  • Save jlandure/514917437b0a4c938e0c to your computer and use it in GitHub Desktop.
Save jlandure/514917437b0a4c938e0c to your computer and use it in GitHub Desktop.
A simple script to count the number of watcher on your angular app. Credits: http://stackoverflow.com/questions/18499909/how-to-count-total-number-of-watches-on-a-page
(function () {
var root = angular.element(document.getElementsByTagName('html'));
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);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment