Skip to content

Instantly share code, notes, and snippets.

@jerry4
Last active August 29, 2015 14:09
Show Gist options
  • Save jerry4/c07ad6e892a1ca30ed74 to your computer and use it in GitHub Desktop.
Save jerry4/c07ad6e892a1ca30ed74 to your computer and use it in GitHub Desktop.
ng app debugging
if (jerry) { alert("already added ngHelpers"); } //
var jerry = (function(){
var count = 0;
var unregisterDigestWatch = function() {}; // init empty
function registerDigestWatch(scope) {
scope = scope || angular.element(document).scope();
unregisterDigestWatch = scope.$watch(function() {
var c = count++;
console.log("Digest " + c +" START...");
var start = performance.now();
scope.$$postDigest(function() {
console.log("Digest " + c + " stoped " + (performance.now() - start));
});
});
}
function steadyDigestTimer() {
var scope = angular.element(document);
scope.injector().invoke( function($rootScope) {
var a = performance.now();
$rootScope.$apply();
console.log(performance.now() - a + " ms");
});
}
function getWatchers(root) {
root = angular.element(root || document.documentElement);
var watcherCount = 0;
function getElemWatchers(element) {
var isolateWatchers = getWatchersFromScope(element.data().$isolateScope);
var scopeWatchers = getWatchersFromScope(element.data().$scope);
var watchers = scopeWatchers.concat(isolateWatchers);
angular.forEach(element.children(), function (childElement) {
watchers = watchers.concat(getElemWatchers(angular.element(childElement)));
});
return watchers;
}
function getWatchersFromScope(scope) {
if (scope) {
return scope.$$watchers || [];
} else {
return [];
}
}
return getElemWatchers(root);
}
return {
registerDigestWatch: registerDigestWatch,
unregisterDigestWatch: function() { unregisterDigestWatch(); },
resetDigestCount: function() { count = 0; },
//registerDigest: registerDigest
steadyDigestTimer: steadyDigestTimer,
getWatchCount: function(scope) { getWatchers(scope).length; }
};
})();
jerry.registerDigestWatch();
jerry.steadyDigestTimer();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment