1- import { DOM } from 'facade/dom' ;
1+ import { benchmark , benchmarkStep } from '../benchpress' ;
2+
3+ import { DOM , document } from 'facade/dom' ;
4+ import { isBlank } from 'facade/lang' ;
5+ import { MapWrapper } from 'facade/collection' ;
6+ import { AnnotatedType } from 'core/compiler/annotated_type' ;
27
38import { Parser } from 'change_detection/parser/parser' ;
49import { ClosureMap } from 'change_detection/parser/closure_map' ;
@@ -11,39 +16,65 @@ import {Component} from 'core/annotations/component';
1116import { Decorator } from 'core/annotations/decorator' ;
1217import { TemplateConfig } from 'core/annotations/template_config' ;
1318
19+ var COUNT = 30 ;
20+
1421var compiler ;
1522var annotatedComponent ;
16- var annotatedComponentNoDirectives ;
17-
18- var emptyTemplate ;
19- var templateWith25ElementsNoBindings ;
20- var templateWith25ElementsAndBindings ;
2123
22- export function setup ( ) {
24+ function setup ( ) {
2325 var closureMap = new ClosureMap ( ) ;
24- var reflector = new Reflector ( ) ;
26+ var reflector = new CachingReflector ( ) ;
2527 compiler = new Compiler ( null , reflector , new Parser ( new Lexer ( ) , closureMap ) , closureMap ) ;
26- annotatedComponent = reflector . annotatedType ( SomeComponent ) ;
27- annotatedComponentNoDirectives = reflector . annotatedType ( ComponentWithNoDirectives ) ;
28-
29- emptyTemplate = createTemplate ( '<div></div>' ) ;
30- templateWith25ElementsNoBindings = buildTemplateWith25ElementsNoBindings ( ) ;
31- templateWith25ElementsAndBindings = buildTemplateWith25ElementsAndBindings ( ) ;
28+ annotatedComponent = reflector . annotatedType ( BenchmarkComponent ) ;
3229}
3330
34- export function compileEmptyTemplate ( ) {
35- var template = emptyTemplate ;
36- return compiler . compileWithCache ( null , annotatedComponent , template ) ;
31+ export function main ( ) {
32+ setup ( ) ;
33+
34+ benchmark ( `Compiler.compile 5*${ COUNT } element no bindings` , function ( ) {
35+ var template = loadTemplate ( 'templateNoBindings' , COUNT ) ;
36+
37+ benchmarkStep ( 'run' , function ( ) {
38+ // Need to clone every time as the compiler might modify the template!
39+ var cloned = DOM . clone ( template ) ;
40+ compiler . compileWithCache ( null , annotatedComponent , cloned ) ;
41+ } ) ;
42+ } ) ;
43+
44+ benchmark ( `Compiler.compile 5*${ COUNT } element with bindings` , function ( ) {
45+ var template = loadTemplate ( 'templateWithBindings' , COUNT ) ;
46+
47+ benchmarkStep ( 'run' , function ( ) {
48+ // Need to clone every time as the compiler might modify the template!
49+ var cloned = DOM . clone ( template ) ;
50+ compiler . compileWithCache ( null , annotatedComponent , cloned ) ;
51+ } ) ;
52+ } ) ;
3753}
3854
39- export function compile25ElementsWithBindings ( ) {
40- var template = templateWith25ElementsAndBindings ;
41- return compiler . compileWithCache ( null , annotatedComponent , template ) ;
55+ function loadTemplate ( templateId , repeatCount ) {
56+ var template = DOM . querySelectorAll ( document , `#${ templateId } ` ) [ 0 ] ;
57+ var content = DOM . getInnerHTML ( template ) ;
58+ var result = '' ;
59+ for ( var i = 0 ; i < repeatCount ; i ++ ) {
60+ result += content ;
61+ }
62+ return DOM . createTemplate ( result ) ;
4263}
4364
44- export function compile25ElementsNoBindings ( ) {
45- var template = templateWith25ElementsNoBindings ;
46- return compiler . compileWithCache ( null , annotatedComponentNoDirectives , template ) ;
65+ // Caching reflector as reflection in Dart using Mirrors
66+ class CachingReflector extends Reflector {
67+ constructor ( ) {
68+ this . _cache = MapWrapper . create ( ) ;
69+ }
70+ annotatedType ( type :Type ) :AnnotatedType {
71+ var result = MapWrapper . get ( this . _cache , type ) ;
72+ if ( isBlank ( result ) ) {
73+ result = super . annotatedType ( type ) ;
74+ MapWrapper . set ( this . _cache , type , result ) ;
75+ }
76+ return result ;
77+ }
4778}
4879
4980@Decorator ( {
@@ -91,79 +122,5 @@ class Dir4 {}
91122 directives : [ Dir0 , Dir1 , Dir2 , Dir3 , Dir4 ]
92123 } )
93124} )
94- class SomeComponent { }
125+ class BenchmarkComponent { }
95126
96- @Component ( {
97- template : new TemplateConfig ( {
98- directives : [ ]
99- } )
100- } )
101- class ComponentWithNoDirectives { }
102-
103- // creates 25 nested divs without bindings, each looking like this:
104- // <div class="class0 class1 class2 class3 class4 " dir0="" attr0="value0" dir1="" attr1="value1" dir2="" attr2="value2" dir3="" attr3="value3" dir4="" attr4="value4">
105- // </div>
106- function buildTemplateWith25ElementsNoBindings ( ) {
107- var result = '' ;
108- for ( var i = 0 ; i < 5 ; i ++ ) {
109- for ( var j = 0 ; j < 5 ; j ++ ) {
110- result += '<div class="' ;
111- for ( var k = 0 ; k < 5 ; k ++ ) {
112- result += `class${ k } ` ;
113- }
114- result += '"' ;
115- for ( var k = 0 ; k < 5 ; k ++ ) {
116- result += ` dir${ k } ` ;
117- result += ` attr${ k } =value${ k } ` ;
118- }
119- for ( var k = 0 ; k < 5 ; k ++ ) {
120- result += ` dir${ k } ` ;
121- result += ` attr${ k } =value${ k } ` ;
122- }
123- result += '>' ;
124- }
125- for ( var j = 0 ; j < 5 ; j ++ ) {
126- result += '</div>' ;
127- }
128- }
129- return createTemplate ( result ) ;
130- }
131-
132- // creates 25 nested divs , each looking like this:
133- // <div class="class0 class1 class2 class3 class4 " dir0="" [attr0]="value0" dir1="" [attr1]="value1" dir2="" [attr2]="value2" dir3="" [attr3]="value3" dir4="" [attr4]="value4">
134- // {{inter0}}{{inter1}}{{inter2}}{{inter3}}{{inter4}}
135- // </div>
136- function buildTemplateWith25ElementsAndBindings ( ) {
137- var result = '' ;
138- for ( var i = 0 ; i < 5 ; i ++ ) {
139- for ( var j = 0 ; j < 5 ; j ++ ) {
140- result += '<div class="' ;
141- for ( var k = 0 ; k < 5 ; k ++ ) {
142- result += `class${ k } ` ;
143- }
144- result += '"' ;
145- for ( var k = 0 ; k < 5 ; k ++ ) {
146- result += ` dir${ k } ` ;
147- result += ` [attr${ k } ]=value${ k } ` ;
148- }
149- for ( var k = 0 ; k < 5 ; k ++ ) {
150- result += ` dir${ k } ` ;
151- result += ` [attr${ k } ]=value${ k } ` ;
152- }
153- result += '>' ;
154- for ( var k = 0 ; k < 5 ; k ++ ) {
155- result += `{{inter${ k } }}` ;
156- }
157- }
158- for ( var j = 0 ; j < 5 ; j ++ ) {
159- result += '</div>' ;
160- }
161- }
162- return createTemplate ( result ) ;
163- }
164-
165-
166-
167- function createTemplate ( html ) {
168- return DOM . createTemplate ( html ) ;
169- }
0 commit comments