Skip to content

Instantly share code, notes, and snippets.

@markgardner
Created March 3, 2016 20:43
Show Gist options
  • Save markgardner/27d55961e0f3683a0019 to your computer and use it in GitHub Desktop.
Save markgardner/27d55961e0f3683a0019 to your computer and use it in GitHub Desktop.
AngularJs: Measure digest timings for a specific scope
// Modify angular.js $rootScope.Scope#$digest. Replace the depth-first traversal chunk with what is below.
// Then within your app assign window.trackScopeId = $scope.$id;
// Insanity Warning: scope depth-first traversal
// yes, this code is a bit crazy, but it works and we have tests to prove it!
// this piece should be kept in sync with the traversal in $broadcast
if (!(next = (current.$$childHead ||
(current !== target && current.$$nextSibling)))) {
while (current !== target && !(next = current.$$nextSibling)) {
if(current.$id === window.trackScopeId && current.$$digestStart) {
var deltaTime = (performance.now() - next.$$digestStart);
if(deltaTime > 1000) {
// debugger;
}
console.log('watch $digest', deltaTime, 'ms ttl', TTL - ttl, 'digest trigger by', this.$id);
next.$$digestStart = null;
}
current = current.$parent;
}
}
if(next && next.$id === window.trackScopeId) {
if(!next.$$digestStart) {
next.$$digestStart = performance.now();
} else {
var deltaTime = (performance.now() - next.$$digestStart);
if(deltaTime > 1000) {
// debugger;
}
console.log('watch $digest', deltaTime, 'ms ttl', TTL - ttl, 'digest trigger by', this.$id);
next.$$digestStart = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment