Skip to content

Instantly share code, notes, and snippets.

@nilesh93
Created April 28, 2017 05:21
Show Gist options
  • Save nilesh93/eec957021e9ec5d7f7abab8f7b995318 to your computer and use it in GitHub Desktop.
Save nilesh93/eec957021e9ec5d7f7abab8f7b995318 to your computer and use it in GitHub Desktop.
Get Angular Watchers count
(function () {
if (!angular) {
console.log('Not a angular application or window.angular not found');
return;
}
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);
var watchersWithoutDuplicates = [];
angular.forEach(watchers, function(item) {
if(watchersWithoutDuplicates.indexOf(item) < 0) {
watchersWithoutDuplicates.push(item);
}
});
console.log('There is ' + watchersWithoutDuplicates.length + ' watcher(s) in the page');
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment