Repository navigation
Expand file tree
/
Copy pathtree_benchmark.js
More file actions
355 lines (306 loc) · 10.1 KB
/
Copy pathtree_benchmark.js
File metadata and controls
355 lines (306 loc) · 10.1 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import {Parser, Lexer, ChangeDetector, ChangeDetection, jitChangeDetection}
from 'angular2/change_detection';
import {bootstrap, Component, Template, TemplateConfig, ViewPort, Compiler} from 'angular2/angular2';
import {CompilerCache} from 'angular2/src/core/compiler/compiler';
import {DirectiveMetadataReader} from 'angular2/src/core/compiler/directive_metadata_reader';
import {TemplateLoader} from 'angular2/src/core/compiler/template_loader';
import {ShadowDomStrategy, NativeShadowDomStrategy} from 'angular2/src/core/compiler/shadow_dom_strategy';
import {LifeCycle} from 'angular2/src/core/life_cycle/life_cycle';
import {reflector} from 'angular2/src/reflection/reflection';
import {DOM, document, window, Element, gc} from 'angular2/src/facade/dom';
import {isPresent} from 'angular2/src/facade/lang';
import {getIntParameter, bindAction} from 'angular2/src/test_lib/benchmark_util';
import {XHR} from 'angular2/src/core/compiler/xhr/xhr';
import {XHRImpl} from 'angular2/src/core/compiler/xhr/xhr_impl';
function setupReflector() {
// TODO: Put the general calls to reflector.register... in a shared file
// as they are needed in all benchmarks...
reflector.registerType(AppComponent, {
'factory': () => new AppComponent(),
'parameters': [],
'annotations' : [new Component({
selector: 'app',
template: new TemplateConfig({
directives: [TreeComponent],
inline: `<tree [data]='initData'></tree>`
})
})]
});
reflector.registerType(TreeComponent, {
'factory': () => new TreeComponent(),
'parameters': [],
'annotations' : [new Component({
selector: 'tree',
bind: {
'data': 'data'
},
template: new TemplateConfig({
directives: [TreeComponent, NgIf],
inline: `<span>{{data.value}}<span template='ng-if data.right != null'><tree [data]='data.right'></tree></span><span template='ng-if data.left != null'><tree [data]='data.left'></tree></span></span>`
})
})]
});
reflector.registerType(NgIf, {
'factory': (vp) => new NgIf(vp),
'parameters': [[ViewPort]],
'annotations' : [new Template({
selector: '[ng-if]',
bind: {
'ng-if': 'ngIf'
}
})]
});
reflector.registerType(Compiler, {
'factory': (cd, templateLoader, reader, parser, compilerCache, strategy) => new Compiler(cd, templateLoader, reader, parser, compilerCache, strategy),
'parameters': [[ChangeDetection], [TemplateLoader], [DirectiveMetadataReader],
[Parser], [CompilerCache], [ShadowDomStrategy]],
'annotations': []
});
reflector.registerType(CompilerCache, {
'factory': () => new CompilerCache(),
'parameters': [],
'annotations': []
});
reflector.registerType(Parser, {
'factory': (lexer) => new Parser(lexer),
'parameters': [[Lexer]],
'annotations': []
});
reflector.registerType(TemplateLoader, {
'factory': (xhr) => new TemplateLoader(xhr),
'parameters': [[XHR]],
'annotations': []
});
reflector.registerType(XHR, {
'factory': () => new XHRImpl(),
'parameters': [],
'annotations': []
});
reflector.registerType(DirectiveMetadataReader, {
'factory': () => new DirectiveMetadataReader(),
'parameters': [],
'annotations': []
});
reflector.registerType(ShadowDomStrategy, {
'factory': () => new NativeShadowDomStrategy(),
'parameters': [],
'annotations': []
});
reflector.registerType(Lexer, {
'factory': () => new Lexer(),
'parameters': [],
'annotations': []
});
reflector.registerType(LifeCycle, {
"factory": (cd) => new LifeCycle(cd),
"parameters": [[ChangeDetector]],
"annotations": []
});
reflector.registerGetters({
'value': (a) => a.value,
'left': (a) => a.left,
'right': (a) => a.right,
'initData': (a) => a.initData,
'data': (a) => a.data
});
reflector.registerSetters({
'value': (a,v) => a.value = v,
'left': (a,v) => a.left = v,
'right': (a,v) => a.right = v,
'initData': (a,v) => a.initData = v,
'data': (a,v) => a.data = v,
'ngIf': (a,v) => a.ngIf = v
});
}
export function main() {
var maxDepth = getIntParameter('depth');
setupReflector();
var app;
var lifeCycle;
var baselineRootTreeComponent;
var count = 0;
function ng2DestroyDom() {
// TODO: We need an initial value as otherwise the getter for data.value will fail
// --> this should be already caught in change detection!
app.initData = new TreeNode('', null, null);
lifeCycle.tick();
}
function profile(create, destroy, name) {
return function() {
window.console.profile(name + ' w GC');
var duration = 0;
var count = 0;
while(count++ < 150) {
gc();
var start = window.performance.now();
create();
duration += window.performance.now() - start;
destroy();
}
window.console.profileEnd(name + ' w GC');
window.console.log(`Iterations: ${count}; time: ${duration / count} ms / iteration`);
window.console.profile(name + ' w/o GC');
duration = 0;
count = 0;
while(count++ < 150) {
var start = window.performance.now();
create();
duration += window.performance.now() - start;
destroy();
}
window.console.profileEnd(name + ' w/o GC');
window.console.log(`Iterations: ${count}; time: ${duration / count} ms / iteration`);
};
}
function ng2CreateDom() {
var values = count++ % 2 == 0 ?
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '*'] :
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', '-'];
app.initData = buildTree(maxDepth, values, 0);
lifeCycle.tick();
}
function noop() {}
function initNg2() {
bootstrap(AppComponent).then((injector) => {
lifeCycle = injector.get(LifeCycle);
app = injector.get(AppComponent);
bindAction('#ng2DestroyDom', ng2DestroyDom);
bindAction('#ng2CreateDom', ng2CreateDom);
bindAction('#ng2UpdateDomProfile', profile(ng2CreateDom, noop, 'ng2-update'));
bindAction('#ng2CreateDomProfile', profile(ng2CreateDom, ng2DestroyDom, 'ng2-create'));
});
}
function baselineDestroyDom() {
baselineRootTreeComponent.update(new TreeNode('', null, null));
}
function baselineCreateDom() {
var values = count++ % 2 == 0 ?
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '*'] :
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', '-'];
baselineRootTreeComponent.update(buildTree(maxDepth, values, 0));
}
function initBaseline() {
var tree = DOM.createElement('tree');
DOM.appendChild(DOM.querySelector(document, 'baseline'), tree);
baselineRootTreeComponent = new BaseLineTreeComponent(tree);
bindAction('#baselineDestroyDom', baselineDestroyDom);
bindAction('#baselineCreateDom', baselineCreateDom);
bindAction('#baselineUpdateDomProfile', profile(baselineCreateDom, noop, 'baseline-update'));
bindAction('#baselineCreateDomProfile', profile(baselineCreateDom, baselineDestroyDom, 'baseline-create'));
}
initNg2();
initBaseline();
}
class TreeNode {
value:string;
left:TreeNode;
right:TreeNode;
constructor(value, left, right) {
this.value = value;
this.left = left;
this.right = right;
}
}
function buildTree(maxDepth, values, curDepth) {
if (maxDepth === curDepth) return new TreeNode('', null, null);
return new TreeNode(
values[curDepth],
buildTree(maxDepth, values, curDepth+1),
buildTree(maxDepth, values, curDepth+1));
}
var BASELINE_TREE_TEMPLATE = DOM.createTemplate(
'<span>_<template class="ng-binding"></template><template class="ng-binding"></template></span>');
var BASELINE_IF_TEMPLATE = DOM.createTemplate(
'<span template="ng-if"><tree></tree></span>');
// http://jsperf.com/nextsibling-vs-childnodes
class BaseLineTreeComponent {
element:Element;
value:BaseLineInterpolation;
left:BaseLineIf;
right:BaseLineIf;
constructor(element) {
this.element = element;
var clone = DOM.clone(BASELINE_TREE_TEMPLATE.content.firstChild);
var shadowRoot = this.element.createShadowRoot();
DOM.appendChild(shadowRoot, clone);
var child = clone.firstChild;
this.value = new BaseLineInterpolation(child);
child = DOM.nextSibling(child);
this.left = new BaseLineIf(child);
child = DOM.nextSibling(child);
this.right = new BaseLineIf(child);
}
update(value:TreeNode) {
this.value.update(value.value);
this.left.update(value.left);
this.right.update(value.right);
}
}
class BaseLineInterpolation {
value:string;
textNode;
constructor(textNode) {
this.value = null;
this.textNode = textNode;
}
update(value:string) {
if (this.value !== value) {
this.value = value;
DOM.setText(this.textNode, value + ' ');
}
}
}
class BaseLineIf {
condition:boolean;
component:BaseLineTreeComponent;
anchor:Element;
constructor(anchor) {
this.anchor = anchor;
this.condition = false;
this.component = null;
}
update(value:TreeNode) {
var newCondition = isPresent(value);
if (this.condition !== newCondition) {
this.condition = newCondition;
if (isPresent(this.component)) {
this.component.element.remove();
this.component = null;
}
if (this.condition) {
var element = DOM.firstChild(DOM.clone(BASELINE_IF_TEMPLATE).content);
this.anchor.parentNode.insertBefore(element, DOM.nextSibling(this.anchor));
this.component = new BaseLineTreeComponent(DOM.firstChild(element));
}
}
if (isPresent(this.component)) {
this.component.update(value);
}
}
}
class AppComponent {
initData:TreeNode;
constructor() {
// TODO: We need an initial value as otherwise the getter for data.value will fail
// --> this should be already caught in change detection!
this.initData = new TreeNode('', null, null);
}
}
// TODO: Move this into a reusable directive in the 'core' module!
class NgIf {
_viewPort:ViewPort;
constructor(viewPort:ViewPort) {
this._viewPort = viewPort;
}
set ngIf(value:boolean) {
if (this._viewPort.length > 0) {
this._viewPort.remove(0);
}
if (value) {
this._viewPort.create();
}
}
}
class TreeComponent {
data:TreeNode;
}