forked from bahmutov/code-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathng-profile-local-digest.js
32 lines (27 loc) · 1.12 KB
/
ng-profile-local-digest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/* measures how long an idle digest cycle (nothing has changed, just running all
dirty checking watchers) takes for a scope surrounding given selector.
Use: run this code snippet, then profileDirectiveDigest('#foo'); to measure
watchers in the #foo element (and its children along the scope tree).
*/
(function (window) {
var performance = window.performance;
function getDiff(start, end) {
return (end - start).toFixed(4);
}
function profileDirectiveDigest(selector) {
console.assert(selector && typeof selector === 'string', 'expected selector', selector);
var el = document.querySelector(selector);
console.assert(el, 'cannot find element with selector', selector);
/* global angular */
var ngEl = angular.element(el);
var scope = ngEl.scope() || ngEl.isolateScope();
var startTime;
var endTime;
console.assert(scope, 'cannot find scope from element', selector);
startTime = performance.now();
scope.$digest();
endTime = performance.now();
console.log(selector, getDiff(startTime, endTime) + ' digest');
}
window.profileDirectiveDigest = profileDirectiveDigest;
}(window));