/
index.ts
67 lines (57 loc) · 2.34 KB
/
index.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
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import {ElementRef, TemplateRef, ViewContainerRef} from '../../../../src/linker';
import {ɵɵdefineDirective, ɵɵdirectiveInject, ɵɵtemplate} from '../../../../src/render3/index';
import {createTNode, createTView} from '../../../../src/render3/instructions/shared';
import {RenderFlags} from '../../../../src/render3/interfaces/definition';
import {TNodeType, TViewNode} from '../../../../src/render3/interfaces/node';
import {injectTemplateRef, injectViewContainerRef} from '../../../../src/render3/view_engine_compatibility';
import {createBenchmark} from '../micro_bench';
import {createAndRenderLView} from '../setup';
class TemplateRefToken {
static __NG_ELEMENT_ID__:
() => TemplateRef<any>| null = () => injectTemplateRef(TemplateRef, ElementRef);
}
class ViewContainerRefToken {
static __NG_ELEMENT_ID__:
() => ViewContainerRef = () => injectViewContainerRef(ViewContainerRef, ElementRef);
}
class NgIfLike {
static ɵfac = () =>
new NgIfLike(ɵɵdirectiveInject(TemplateRefToken), ɵɵdirectiveInject(ViewContainerRefToken));
static ɵdir = ɵɵdefineDirective({
type: NgIfLike,
selectors: [['', 'viewManipulation', '']],
});
constructor(private tplRef: TemplateRefToken, private vcRef: ViewContainerRefToken) {}
}
`
<ng-template viewManipulation></ng-template>
<ng-template viewManipulation></ng-template>
`;
function testTemplate(rf: RenderFlags, ctx: any) {
if (rf & 1) {
ɵɵtemplate(0, null, 0, 0, 'ng-template', 0);
ɵɵtemplate(1, null, 0, 0, 'ng-template', 0);
}
}
const viewTNode = createTNode(null !, null, TNodeType.View, -1, null, null) as TViewNode;
const embeddedTView = createTView(
-1, testTemplate, 2, 0, [NgIfLike.ɵdir], null, null, null, [['viewManipulation', '']]);
// create view once so we don't profile first template pass
createAndRenderLView(null, embeddedTView, viewTNode);
// scenario to benchmark
const elementTextCreate = createBenchmark('ng_template');
const createTime = elementTextCreate('create');
console.profile('ng_template_create');
while (createTime()) {
createAndRenderLView(null, embeddedTView, viewTNode);
}
console.profileEnd();
// report results
elementTextCreate.report();