Skip to content

Instantly share code, notes, and snippets.

@nicksheffield
Created August 16, 2017 06:16
Show Gist options
  • Save nicksheffield/2d57a14691db6d0beb064845c460de39 to your computer and use it in GitHub Desktop.
Save nicksheffield/2d57a14691db6d0beb064845c460de39 to your computer and use it in GitHub Desktop.
Count amount of watchers in angularjs
function getScopes(root) {
var scopes = []
function traverse(scope) {
scopes.push(scope)
if (scope.$$nextSibling)
traverse(scope.$$nextSibling)
if (scope.$$childHead)
traverse(scope.$$childHead)
}
traverse(root)
return scopes
}
var rootScope = angular.element(document.querySelectorAll('[ng-app]')).scope()
var scopes = getScopes(rootScope)
var watcherLists = scopes.map(s => s.$$watchers)
_.uniq(_.flatten(watcherLists)).length
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment