Last active
August 29, 2015 14:08
-
-
Save pxwise/e1f8227288f889a49775 to your computer and use it in GitHub Desktop.
Utility to count watchers in an AngularJS app. The higher the number of watchers, the slower your app will run.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Utility to count watchers in an AngularJS app. | |
*/ | |
function countWatchers() { | |
var root = angular.element(document.getElementsByTagName('body')); | |
var watchers = []; | |
var f = function (element) { | |
if (element.data().hasOwnProperty('$scope')) { | |
angular.forEach(element.data().$scope.$$watchers, function(watcher) { | |
watchers.push(watcher); | |
}); | |
} | |
angular.forEach(element.children(), function(childElement) { | |
f(angular.element(childElement)); | |
}); | |
}; | |
f(root); | |
console.log('watchers.length: ', watchers.length); | |
} | |
(function() { | |
countWatchersTimeout = window.setTimeout(countWatchers, 10000); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment