-
Notifications
You must be signed in to change notification settings - Fork 199
/
ng-scope-size.js
105 lines (85 loc) · 2.68 KB
/
ng-scope-size.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Finds total size of objects attached to the scopes
(function ngScopeSize() {
var i, data, scope,
count = 0,
all = document.all,
len = all.length,
test = {},
scopes = 0;
/*
sizeof.js modified for angular scope client data computation
A function to calculate the approximate memory usage of objects
Created by Stephen Morley - http://code.stephenmorley.org/ - and released under
the terms of the CC0 1.0 Universal legal code:
http://creativecommons.org/publicdomain/zero/1.0/legalcode
*/
/* Returns the approximate memory usage, in bytes, of the specified object. The
* parameter is:
*
* object - the object whose size should be determined
*/
/* jshint -W073, -W071 */
function sizeof(object){
// initialise the list of objects and size
var objects = [object];
var size = 0;
// loop over the objects
for (var index = 0; index < objects.length; index += 1) {
// determine the type of the object
switch (typeof objects[index]) {
case 'boolean': {
size += 4; break;
}
case 'number': {
size += 8; break;
}
case 'string': {
size += 2 * objects[index].length;
break;
}
case 'object': {
// loop over the keys
for (var key in objects[index]) {
if (!objects[index].hasOwnProperty(key)) {
continue;
}
if (key[0] === '$' || key === 'this' || key === 'constructor' || key === 'lenth') {
continue; // angular's internal property ($apply, etc)
}
// determine whether the value has already been processed
var processed = false;
/* jshint -W073 */
for (var search = 0; search < objects.length; search += 1){
if (objects[search] === objects[index][key]){
processed = true;
break;
}
}
// queue the value to be processed if appropriate
if (!processed) {
objects.push(objects[index][key]);
}
}
}
}
}
// return the calculated size
return size;
}
// go through each element. Count watchers if it has scope or isolate scope
/* eslint no-for-loops:0 */
for (i = 0; i < len; i++) {
/* global angular */
data = angular.element(all[i]).data();
scope = data.$scope || data.$isolateScope;
if (scope) {
if ( !test[ scope.$id ] ) {
test[ scope.$id ] = true;
count += sizeof(scope);
scopes += 1;
}
}
}
console.log(scopes, 'scopes have', count, 'bytes attached');
return count;
}());