Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.