Skip to content

Commit 1406bb7

Browse files
committed
feat(ng): counting angular watchers in a tree
1 parent 16cf3bf commit 1406bb7

File tree

3 files changed

+42
-12
lines changed

3 files changed

+42
-12
lines changed

bower.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "code-snippets",
33
"main": "first-paint.js",
4-
"version": "0.5.0",
4+
"version": "0.6.0",
55
"homepage": "https://github.com/bahmutov/code-snippets",
66
"license": "MIT",
77
"ignore": [

ng-count-watchers.js

+39-9
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,35 @@
11
// 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+
320
var i, data, scope,
421
count = 0,
522
all = document.all,
6-
len = all.length,
723
test = {},
824
watchers = [];
925

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+
1033
var mostWatchers = 0;
1134
var maxWatchersToPrint = 20;
1235

@@ -21,20 +44,27 @@
2144
console.log('most watchers', n);
2245
console.log(element);
2346
mostWatchers = n;
47+
2448
}
2549
}
2650

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) {
3052
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+
}
3559
}
3660
}
3761
}
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+
}
3868
console.log('this page has ' + watchers.length + ' angular watchers');
3969
if (watchers.length < maxWatchersToPrint) {
4070
console.log('the watchers are:', watchers);

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "code-snippets",
33
"description": "Chrome DevTools code snippets ",
4-
"version": "0.5.0",
4+
"version": "0.6.0",
55
"author": "Gleb Bahmutov <[email protected]>",
66
"bugs": {
77
"url": "https://github.com/bahmutov/code-snippets/issues"
@@ -69,4 +69,4 @@
6969
"size": "t=\"$(npm pack .)\"; wc -c \"${t}\"; tar tvf \"${t}\"; rm \"${t}\";",
7070
"test": "grunt"
7171
}
72-
}
72+
}

0 commit comments

Comments
 (0)