Skip to content

Instantly share code, notes, and snippets.

@kratob
Created March 20, 2015 17:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kratob/5b8ab643abab86154c18 to your computer and use it in GitHub Desktop.
Save kratob/5b8ab643abab86154c18 to your computer and use it in GitHub Desktop.
Bookmarklet to count angular watchers
javascript: (function() {
function getWatchCount() {
var total = 0;
var scopeIds = {};
angular.forEach(
document.querySelectorAll( ".ng-scope , .ng-isolate-scope" ),
countWatchersInNode
);
return( total );
function countWatchersInNode( node ) {
var element = angular.element( node );
if ( element.hasClass( "ng-isolate-scope" ) && element.isolateScope ) {
countWatchersInScope( element.isolateScope() );
}
if ( element.hasClass( "ng-scope" ) ) {
countWatchersInScope( element.scope() );
}
}
function countWatchersInScope( scope ) {
if ( scopeIds.hasOwnProperty( scope.$id ) ) {
return;
}
scopeIds[ scope.$id ] = true;
if ( scope.$$watchers ) {
total += scope.$$watchers.length;
}
}
}
console.log("TOTAL WATCHERS: " + getWatchCount());
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment