1
+ var app = angular . module ( 'jsBenchmark' , [ ] ) ;
2
+
3
+ function runner ( func , n ) {
4
+ n = n || 5000 ;
5
+ return function theRunner ( ) {
6
+ for ( var i = 0 ; i < n ; i ++ ) {
7
+ func ( ) ;
8
+ }
9
+ } ;
10
+ }
11
+
12
+ /* copy */
13
+ app . controller ( 'DataController' , [ '$compile' , function ( $compile ) {
14
+ var COPY_BASIC = {
15
+ i : 123 ,
16
+ str : "foo" ,
17
+ date : new Date ( ) ,
18
+ sub : { event : "foo" }
19
+ } ;
20
+ var COPY_ARRAY_BASIC = [ 123 , "foo" , new Date ( ) , { event : "foo" } ] ;
21
+ var COPY_ARRAY_SIMPLE = [ 123 , "foo" , new Date ( ) , / f o o / ] ;
22
+
23
+ var BIG_ARRAY_OBJECTS = new Array ( 50 ) . join ( " " ) . split ( " " ) . map ( function ( s , i ) {
24
+ return {
25
+ i : i ,
26
+ s : s + i ,
27
+ o : { foo : "bar" , date : new Date ( ) } ,
28
+ a : [ / f o o / , new Int32Array ( ) ]
29
+ } ;
30
+ } ) ;
31
+
32
+ var BIG_COMPLICATED_ARRAY_OBJECTS = new Array ( 50 ) . join ( " " ) . split ( " " ) . reduce ( function ( a , s , i ) {
33
+ var o = { } ;
34
+
35
+ o . i = i ;
36
+ o . s = s + i ;
37
+ o . d = ( i % 5 === 1 ) ? a [ i - 1 ] . d : new Date ( ) ;
38
+ o . re = ( i % 5 === 2 ) ? a [ i - 2 ] . re : / f o o b a r / i;
39
+ o . a = ( i % 5 === 3 ) ? a [ i - 3 ] . a : [ 'foo' , 123 ] ;
40
+
41
+ if ( i % 5 === 0 ) {
42
+ o . o = o ;
43
+ } else {
44
+ o . o = { foo : "bar" } ;
45
+ }
46
+ if ( i % 4 === 1 ) {
47
+ o . p = a [ i - 1 ] ;
48
+ }
49
+
50
+ a . push ( o ) ;
51
+ if ( i % 20 === 0 ) {
52
+ a . push ( o ) ;
53
+ }
54
+
55
+ return a ;
56
+ } , [ ] ) ;
57
+
58
+ function copy ( what ) {
59
+ for ( var i = 0 ; i < 1000 ; i ++ ) {
60
+ angular . copy ( what ) ;
61
+ }
62
+ }
63
+
64
+ benchmarkSteps . push ( {
65
+ name : 'large array of objects' ,
66
+ fn : copy . bind ( null , BIG_ARRAY_OBJECTS )
67
+ } ) ;
68
+ benchmarkSteps . push ( {
69
+ name : 'large array of over complicated objects' ,
70
+ fn : copy . bind ( null , BIG_COMPLICATED_ARRAY_OBJECTS )
71
+ } ) ;
72
+ benchmarkSteps . push ( {
73
+ name : 'basic + date + 1 recurse' ,
74
+ fn : copy . bind ( null , COPY_BASIC )
75
+ } ) ;
76
+ benchmarkSteps . push ( {
77
+ name : 'basic array + date + 1 recurse' ,
78
+ fn : copy . bind ( null , COPY_ARRAY_BASIC )
79
+ } ) ;
80
+ benchmarkSteps . push ( {
81
+ name : 'simple' ,
82
+ fn : copy . bind ( null , COPY_ARRAY_SIMPLE )
83
+ } ) ;
84
+ } ] ) ;
85
+ // */
86
+
87
+ /* Q * /
88
+ app.controller('DataController', ['$q', function($q) {
89
+ var P = $q.resolve(42);
90
+ var rP = function() { return P; };
91
+
92
+ benchmarkSteps.push({
93
+ name: 'resolve(1)',
94
+ fn: runner(function() {
95
+ return $q.resolve(1);
96
+ })
97
+ });
98
+ benchmarkSteps.push({
99
+ name: 'reject(1)',
100
+ fn: runner(function() {
101
+ return $q.reject(1);
102
+ })
103
+ });
104
+ benchmarkSteps.push({
105
+ name: 'defer.resolve(1)',
106
+ fn: runner(function() {
107
+ return $q.defer().resolve(1);
108
+ })
109
+ });
110
+ benchmarkSteps.push({
111
+ name: 'defer.reject(1)',
112
+ fn: runner(function() {
113
+ return $q.defer().reject(1); //3206 => 3285 (because notify gets wrapped twice now??)
114
+ })
115
+ });
116
+
117
+ benchmarkSteps.push({
118
+ name: 'resolve(P)',
119
+ fn: runner(function() {
120
+ return $q.resolve(P); //way down, but still high retained memory
121
+ })
122
+ });
123
+ benchmarkSteps.push({
124
+ name: 'defer.resolve(P)',
125
+ fn: runner(function() {
126
+ return $q.defer().resolve(P); //way down, but still high retained memory
127
+ })
128
+ });
129
+
130
+ benchmarkSteps.push({
131
+ name: '.then()',
132
+ fn: runner(function() {
133
+ return P.then();
134
+ })
135
+ });
136
+ benchmarkSteps.push({
137
+ name: '.then(rP)',
138
+ fn: runner(function() {
139
+ return P.then(rP);
140
+ })
141
+ });
142
+
143
+ benchmarkSteps.push({
144
+ name: 'Q(noop)',
145
+ fn: runner(function() {
146
+ return $q(angular.noop);
147
+ })
148
+ });
149
+ }]);
150
+ // */
151
+
152
+ /* $compile * /
153
+ app.controller('DataController', ['$compile', '$rootScope', function($compile, $rootScope) {
154
+ var HTML = document.querySelector(".container-fluid").innerHTML;
155
+
156
+
157
+ var i=0;
158
+ HTML = HTML.replace(/(<[a-z]+)/g, function(a, r) {
159
+ if (r % 5) {
160
+ r += ' ng-if="B"';
161
+ } else if (r % 11) {
162
+ r += ' ng-repeat="b in A"';
163
+ } else if (r % 2) {
164
+ r += ' ng-model="b"'
165
+ }
166
+
167
+ i++;
168
+ return r;
169
+ });
170
+
171
+ HTML = HTML.replace(/></g, " ng-bind='C'><");
172
+
173
+
174
+ var step;
175
+ benchmarkSteps.push({
176
+ name: '$compile',
177
+ fn: function() {
178
+ step = $compile(HTML);
179
+ }
180
+ });
181
+ benchmarkSteps.push({
182
+ name: 'link',
183
+ fn: function() {
184
+ step = step(angular.extend(scope = $rootScope.$new(), {
185
+ A: [1,2,3,4,5],
186
+ C: 123
187
+ }));
188
+ }
189
+ });
190
+ benchmarkSteps.push({
191
+ name: 'remove',
192
+ fn: function() {
193
+ step.remove();
194
+ step = null;
195
+
196
+ scope.$destroy();
197
+ scope = null;
198
+ }
199
+ });
200
+ }]);
201
+ // */
202
+
203
+
204
+ /* compiler * /
205
+ app.directive('templateUrl', ['$templateCache', function($templateCache) {
206
+ $templateCache.put('template', '<span>');
207
+
208
+ var VAL = 0;
209
+
210
+ return {
211
+ templateUrl: 'template',
212
+ compile: function($el, $at) {
213
+ $el.text("{{val}}");
214
+ return function($scope, $el) {
215
+ $scope.val = VAL++;
216
+ };
217
+ }
218
+ };
219
+ }]);
220
+
221
+ app.controller('DataController', ['$compile', '$rootScope', function($compile, $rootScope) {
222
+ var HTML = '<aside><div ng-repeat="i in a track by $index"><template-url/></div></aside>';
223
+
224
+ var step, scope;
225
+
226
+ benchmarkSteps.push({
227
+ name: '$compile',
228
+ fn: function() {
229
+ step = angular.element(HTML);
230
+ angular.element(document.body).append(step);
231
+ step = $compile(step);
232
+ }
233
+ });
234
+
235
+ benchmarkSteps.push({
236
+ name: 'link',
237
+ fn: function() {
238
+ scope = $rootScope.$new();
239
+ scope.a = new Array(5000).join(" ").split(" ");
240
+ step = step(scope);
241
+ $rootScope.$apply();
242
+ }
243
+ });
244
+
245
+ benchmarkSteps.push({
246
+ name: '$destroy',
247
+ fn: function() {
248
+ scope.$destroy();
249
+ step.remove();
250
+ scope = step = null;
251
+ }
252
+ });
253
+ }]);
254
+ // */
255
+
256
+ angular . bootstrap ( document . querySelector ( "[ng-app-foo]" ) , [ "jsBenchmark" ] ) ;
0 commit comments