Created
May 5, 2015 16:31
-
-
Save bbrown/9f438a53cca2c65cb0fc to your computer and use it in GitHub Desktop.
Snippet to count watches in Angular (from http://stackoverflow.com/questions/18499909/how-to-count-total-number-of-watches-on-a-page)
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 () { | |
| 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); | |
| // Remove duplicate watchers | |
| var watchersWithoutDuplicates = []; | |
| angular.forEach(watchers, function(item) { | |
| if(watchersWithoutDuplicates.indexOf(item) < 0) { | |
| watchersWithoutDuplicates.push(item); | |
| } | |
| }); | |
| console.log(watchersWithoutDuplicates.length); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment