Skip to content

Instantly share code, notes, and snippets.

@JohannesFerner
Created March 25, 2016 13:27
Show Gist options
  • Save JohannesFerner/eee0841fd93abe48d33a to your computer and use it in GitHub Desktop.
Save JohannesFerner/eee0841fd93abe48d33a to your computer and use it in GitHub Desktop.
'use strict';
angular.module('app.main')
.run(function ($rootScope, $timeout, $log) {
function countAngularWatchers() {
var i, data, scope,
count = 0,
all = document.all,
len = all.length,
test = {};
// go through each element. Count watchers if it has scope or isolate scope
/* eslint no-for-loops:0 */
for (i = 0; i < len; i++) {
/* global angular */
data = angular.element(all[i]).data();
scope = data.$scope || data.$isolateScope;
if (scope && scope.$$watchers) {
if (!test[scope.$id]) {
test[scope.$id] = true;
count += scope.$$watchers.length;
}
}
}
$log.debug('this page has', count, 'angular watchers');
return count;
}
$rootScope.$on('$stateChangeSuccess', function (event) {
$timeout(function () {
countAngularWatchers();
}, 250);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment