|
1 | 1 | // taken from http://ng.malsup.com/#!/counting-watchers
|
2 |
| -(function countAngularWatchers(angular) { |
| 2 | +/* |
| 3 | + Usage |
| 4 | + - just run the script and it will compute number of watchers on the page |
| 5 | + - select an element on the page using "Elements" tab |
| 6 | + and run `countAngularWatchers(angular, $0)` to count total watchers |
| 7 | + in the tree rooted at the selected element. Useful to find where the |
| 8 | + large numbers of watchers are "hiding" |
| 9 | +*/ |
| 10 | +(function countAngularWatchers(angular, start) { |
| 11 | + window.countAngularWatchers = countAngularWatchers; |
| 12 | + |
| 13 | + function allDescendents(list, node) { |
| 14 | + list.push(node); |
| 15 | + Array.prototype.forEach.call(node.childNodes, function (child) { |
| 16 | + allDescendents(list, child); |
| 17 | + }); |
| 18 | + } |
| 19 | + |
3 | 20 | var i, data, scope,
|
4 | 21 | count = 0,
|
5 | 22 | all = document.all,
|
6 |
| - len = all.length, |
7 | 23 | test = {},
|
8 | 24 | watchers = [];
|
9 | 25 |
|
| 26 | + if (start) { |
| 27 | + all = []; |
| 28 | + allDescendents(all, start); |
| 29 | + } |
| 30 | + console.log('counting watchers in', all.length, 'elements'); |
| 31 | + var len = all.length; |
| 32 | + |
10 | 33 | var mostWatchers = 0;
|
11 | 34 | var maxWatchersToPrint = 20;
|
12 | 35 |
|
|
21 | 44 | console.log('most watchers', n);
|
22 | 45 | console.log(element);
|
23 | 46 | mostWatchers = n;
|
| 47 | + |
24 | 48 | }
|
25 | 49 | }
|
26 | 50 |
|
27 |
| - // go through each element. Count watchers if it has scope or isolate scope |
28 |
| - for (i = 0; i < len; i += 1) { |
29 |
| - var el = angular.element(all[i]); |
| 51 | + function countWatchersInData(el) { |
30 | 52 | data = el.data();
|
31 |
| - scope = data.$scope || data.$isolateScope; |
32 |
| - if (scope && scope.$$watchers) { |
33 |
| - if ( !test[ scope.$id ] ) { |
34 |
| - countScopeWatchers(scope, el); |
| 53 | + if (data) { |
| 54 | + scope = data.$scope || data.$isolateScope; |
| 55 | + if (scope && scope.$$watchers) { |
| 56 | + if (!test[scope.$id]) { |
| 57 | + countScopeWatchers(scope, el); |
| 58 | + } |
35 | 59 | }
|
36 | 60 | }
|
37 | 61 | }
|
| 62 | + |
| 63 | + // go through each element. Count watchers if it has scope or isolate scope |
| 64 | + for (i = 0; i < len; i += 1) { |
| 65 | + var el = angular.element(all[i]); |
| 66 | + countWatchersInData(el); |
| 67 | + } |
38 | 68 | console.log('this page has ' + watchers.length + ' angular watchers');
|
39 | 69 | if (watchers.length < maxWatchersToPrint) {
|
40 | 70 | console.log('the watchers are:', watchers);
|
|
0 commit comments