Last active
August 29, 2015 14:19
-
-
Save MarkoCen/62822d7622fa0b0a6c69 to your computer and use it in GitHub Desktop.
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.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