Created
February 17, 2016 08:42
-
-
Save kasperlewau/f0c0d164b9ac01a355bf to your computer and use it in GitHub Desktop.
angular snippets
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
/** | |
* Pass rootElement ($0), and rounds. Default amount of rounds is 1000. | |
* | |
* Don't bump it up to say a million rounds over a set of 5000 watchers, it will take a *long* time. | |
*/ | |
function idleDigestTime (root, rounds) { | |
var results = []; | |
angular.element(root).injector().invoke(function ($rootScope) { | |
for (var i = 0; i < (rounds || 1000); i++) { | |
var x = performance.now(); | |
$rootScope.$apply(); | |
results.push(performance.now() - x); | |
} | |
}); | |
var sum = results.reduce(function (acc, n) { | |
return acc + n; | |
}); | |
console.log('Your average idle $digest time is: ', (sum / results.length).toFixed(3), ' ms'); | |
} |
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 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment