Skip to content

Instantly share code, notes, and snippets.

@tilmanschweitzer
Last active August 29, 2015 14:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tilmanschweitzer/1bad4f98c0c986fa78e1 to your computer and use it in GitHub Desktop.
Save tilmanschweitzer/1bad4f98c0c986fa78e1 to your computer and use it in GitHub Desktop.
angular - analyse scopes and watchers
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