Last active
December 14, 2016 12:25
-
-
Save davinkevin/5cdcbb526faf3a0e03d1 to your computer and use it in GitHub Desktop.
Count the number of watchers in AngularJS 1 include in bookmarklet
This file contains 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
javascript:(function() { | |
if (!angular) { | |
alert('Not a angular application or window.angular not found'); | |
return; | |
} | |
var entryNode = document.querySelector('[ng-app]') || document.querySelector('.ng-scope'); | |
if (!entryNode) { | |
alert('No entryPoint found'); | |
return; | |
} | |
angular.element(entryNode) | |
.injector() | |
.invoke(function($rootScope) { | |
var a = performance.now(); | |
$rootScope.$apply(); | |
alert('A digest cycle took ' + (performance.now()-a).toString() + ' ms '); | |
}) | |
})(); |
This file contains 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
javascript:(function () { | |
if (!angular) { | |
alert('Not a angular application or window.angular not found'); | |
return; | |
} | |
var root = angular.element(document.getElementsByTagName('body')); | |
var watchers = []; | |
var f = function (element) { | |
angular.forEach(['$scope', '$isolateScope'], function (scopeProperty) { | |
if (element.data() && element.data().hasOwnProperty(scopeProperty)) { | |
angular.forEach(element.data()[scopeProperty].$$watchers, function (watcher) { | |
watchers.push(watcher); | |
}); | |
} | |
}); | |
angular.forEach(element.children(), function (childElement) { | |
f(angular.element(childElement)); | |
}); | |
}; | |
f(root); | |
var watchersWithoutDuplicates = []; | |
angular.forEach(watchers, function(item) { | |
if(watchersWithoutDuplicates.indexOf(item) < 0) { | |
watchersWithoutDuplicates.push(item); | |
} | |
}); | |
alert('There is ' + watchersWithoutDuplicates.length + ' watcher(s) in the page'); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment