forked from NativeScript/nativescript-angular
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.ts
128 lines (112 loc) · 3.69 KB
/
benchmark.ts
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
import {Inject, Component, View, NgIf, NgFor} from 'angular2/angular2';
import {ApplicationRef} from 'angular2/src/core/application_ref';
import {LifeCycle} from 'angular2/src/core/life_cycle/life_cycle';
import * as profiling from './profiling.ts';
@Component({selector: 'tree', inputs: ['data']})
@View({
directives: [TreeComponent, NgIf],
template:
`<div>
<span>{{data.value}}</span>
<div *ng-if="data.right != null">
<tree [data]='data.right'></tree>
</div>
<div *ng-if="data.left != null">
<tree [data]='data.left'></tree>
</div>
</div>
`
})
class TreeComponent {
data: TreeNode;
}
@Component({
selector: 'benchmark',
})
@View({
directives: [NgIf, NgFor, TreeComponent],
template: `
<div>
<h1>Benchmark!</h1>
<input type="button" value="Baseline test" (click)="baselineTest(baseline)">
<div #baseline></div>
<input type="button" value="Component test" (click)="componentTest()">
<div #component>
<tree [data]='initDataNg'></tree>
</div>
</div>
`,
})
export class Benchmark {
private count: number = 0;
private maxDepth: number = 10;
public initDataNg = new TreeNode('', null, null);
public initDataBaseline = new TreeNode('', null, null);
constructor(private appRef: ApplicationRef, private lifeCycle: LifeCycle) {
}
public baselineTest(container: Node) {
this.createBaselineDom();
profiling.start('baseline');
this.renderBaselineNode(container, this.initDataBaseline);
profiling.stop('baseline');
//container.innerHTML = '';
}
public componentTest() {
this.initDataNg = new TreeNode('', null, null);
this.lifeCycle.tick();
profiling.start('angular');
this.createNgDom();
profiling.stop('angular');
}
private renderBaselineNode(container: Node, node: TreeNode) {
let span = document.createElement('span');
span.innerHTML = node.value;
container.appendChild(span);
if (node.left) {
let childContainer = document.createElement('div');
container.appendChild(childContainer);
this.renderBaselineNode(childContainer, node.left);
}
if (node.right) {
let childContainer = document.createElement('div');
container.appendChild(childContainer);
this.renderBaselineNode(childContainer, node.right);
}
}
private createBaselineDom() {
var values = this.count++ % 2 == 0 ? ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '*'] :
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', '-'];
this.initDataBaseline = buildTree(this.maxDepth, values, 0);
this.lifeCycle.tick();
}
private createNgDom() {
var values = this.count++ % 2 == 0 ? ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '*'] :
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', '-'];
this.initDataNg = buildTree(this.maxDepth, values, 0);
this.lifeCycle.tick();
}
}
class TreeNode {
value: string;
left: TreeNode;
right: TreeNode;
constructor(value, left, right) {
this.value = value;
this.left = left;
this.right = right;
}
}
var nodes = 0;
function buildTree(maxDepth, values, curDepth) {
if (curDepth === 0) {
nodes = 0;
} else {
nodes++;
}
if (maxDepth === curDepth) return new TreeNode('', null, null);
let result = new TreeNode(values[curDepth], buildTree(maxDepth, values, curDepth + 1), buildTree(maxDepth, values, curDepth + 1));
if (curDepth === 0) {
console.log(`${nodes} nodes created.`);
}
return result;
}