Skip to content

Instantly share code, notes, and snippets.

@MarkoCen
Last active August 29, 2015 14:19
Show Gist options
  • Save MarkoCen/62822d7622fa0b0a6c69 to your computer and use it in GitHub Desktop.
Save MarkoCen/62822d7622fa0b0a6c69 to your computer and use it in GitHub Desktop.
(function () {
var root = angular.element(document.querySelectorAll("body"));
var totalWatchers = [];
//通过data()方法找到DOM对应的$scope, 轮询所有子$scope的$$watchers数量
var calcWatcher = function (element) {
angular.forEach(['$scope', '$isolateScope'], function (scope) {
if (element.data() && element.data().hasOwnProperty(scope)) {
angular.forEach(element.data()[scope].$$watchers, function (watcher) {
totalWatchers.push(watcher);
});
}
});
angular.forEach(element.children(), function (childElement) {
calcWatcher(angular.element(childElement));
});
};
calcWatcher(root);
// Remove duplicate watchers
var watchersWithoutDuplicates = [];
angular.forEach(totalWatchers, function(item) {
if(watchersWithoutDuplicates.indexOf(item) < 0) {
watchersWithoutDuplicates.push(item);
}
});
console.log('Total $$watcher is')
console.log(watchersWithoutDuplicates.length);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment