Last active
August 29, 2015 14:25
-
-
Save tilmanschweitzer/1bad4f98c0c986fa78e1 to your computer and use it in GitHub Desktop.
angular - analyse scopes and watchers
This file contains hidden or 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
function analyseScopeAndWatchers() { | |
// fetch all elements with ng-scope class | |
var scopeElements = Array.prototype.slice.apply(document.querySelectorAll(".ng-scope")); | |
// map elements to scopes | |
var scopes = scopeElements.map(function (element) { | |
return angular.element(element).scope(); | |
}); | |
// filter duplicated scopes created by ng-view | |
var scopesById = {}; | |
var uniqueScopes = []; | |
scopes.forEach(function (scope) { | |
if (scopesById[scope.$id] === undefined) { | |
scopesById[scope.$id] = scope; | |
uniqueScopes.push(scope); | |
} | |
}); | |
// map uniqueScopes to watchers | |
var watchers = uniqueScopes.map(function (scope) { | |
return scope.$$watchers; | |
}).filter(function (watchers) { | |
return watchers != null; | |
}); | |
// extract the count values | |
var watchersCountValues = watchers.map(function (watcher) { | |
return watcher.length; | |
}); | |
// sum up the length with reduce | |
var watchersCount = watchersCountValues.reduce(function(a,b) { | |
return a + b; | |
}); | |
return { | |
watchers: watchers, | |
watchersCount: watchersCount, | |
uniqueScopes: uniqueScopes | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment