Skip to content

Instantly share code, notes, and snippets.

@a-laughlin
Created August 4, 2014 13:32
Show Gist options
  • Save a-laughlin/de797c4090df84ae5b80 to your computer and use it in GitHub Desktop.
Save a-laughlin/de797c4090df84ae5b80 to your computer and use it in GitHub Desktop.
Bookmarklet to count AngularJS watches
// Instructions - create a bookmark. Save this code as the url.
// Click the bookmarklet to see watch count output in the console.
// Currently counts watches by nodeName. It would be more useful by class. TBD.
javascript: (function () {
var totalWatches = 0;
var watchesByElem = {};
angular.forEach(angular.element('.ng-scope'), function (elem) {
var s = angular.element(elem).scope();
var w = s.$$watchers;
if (s.$$watchers) {
watchesByElem[elem.nodeName] = watchesByElem[elem.nodeName]||0;
totalWatches += w.length;
watchesByElem[elem.nodeName] += w.length;
}
});
console.log('Total Watches # ',totalWatches);
console.log('Breakdown by nodeName:');
for(var name in watchesByElem){
console.log(name,watchesByElem[name]);
}
})();
@grantgeorge
Copy link

👍

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