| 
									
										
										
										
											2014-12-05 16:26:30 -08:00
										 |  |  | import {benchmark, benchmarkStep} from 'benchpress/benchpress'; | 
					
						
							| 
									
										
										
										
											2014-11-17 11:01:21 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import {DOM, document} from 'facade/dom'; | 
					
						
							| 
									
										
										
										
											2014-12-05 16:26:30 -08:00
										 |  |  | import {isBlank, Type} from 'facade/lang'; | 
					
						
							| 
									
										
										
										
											2014-11-17 11:01:21 -08:00
										 |  |  | import {MapWrapper} from 'facade/collection'; | 
					
						
							|  |  |  | import {AnnotatedType} from 'core/compiler/annotated_type'; | 
					
						
							| 
									
										
										
										
											2014-11-14 14:03:03 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | import {Parser} from 'change_detection/parser/parser'; | 
					
						
							|  |  |  | import {Lexer} from 'change_detection/parser/lexer'; | 
					
						
							| 
									
										
										
										
											2014-11-17 20:47:05 -08:00
										 |  |  | import {ProtoRecordRange} from 'change_detection/record_range'; | 
					
						
							| 
									
										
										
										
											2014-11-14 14:03:03 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-02 13:21:39 -08:00
										 |  |  | import {Compiler, CompilerCache} from 'core/compiler/compiler'; | 
					
						
							| 
									
										
										
										
											2014-11-20 12:07:48 -08:00
										 |  |  | import {DirectiveMetadataReader} from 'core/compiler/directive_metadata_reader'; | 
					
						
							| 
									
										
										
										
											2014-11-14 14:03:03 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-21 21:19:23 -08:00
										 |  |  | import {Component} from 'core/annotations/annotations'; | 
					
						
							|  |  |  | import {Decorator} from 'core/annotations/annotations'; | 
					
						
							| 
									
										
										
										
											2014-11-14 14:03:03 -08:00
										 |  |  | import {TemplateConfig} from 'core/annotations/template_config'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-17 20:47:05 -08:00
										 |  |  | import {reflector} from 'reflection/reflection'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-17 11:01:21 -08:00
										 |  |  | var COUNT = 30; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-14 14:03:03 -08:00
										 |  |  | var compiler; | 
					
						
							|  |  |  | var annotatedComponent; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-17 11:01:21 -08:00
										 |  |  | function setup() { | 
					
						
							| 
									
										
										
										
											2014-11-17 20:47:05 -08:00
										 |  |  |   reflector.registerType(BenchmarkComponent, { | 
					
						
							|  |  |  |     "factory": () => new BenchmarkComponent(), | 
					
						
							|  |  |  |     "parameters": [], | 
					
						
							|  |  |  |     "annotations" : [new Component({template: new TemplateConfig({directives: [Dir0, Dir1, Dir2, Dir3, Dir4]})})] | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   reflector.registerType(Dir0, { | 
					
						
							|  |  |  |     "factory": () => new Dir0(), | 
					
						
							|  |  |  |     "parameters": [], | 
					
						
							|  |  |  |     "annotations" : [new Decorator({selector: '[dir0]', bind: {'attr0': 'prop'}})] | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   reflector.registerType(Dir1, { | 
					
						
							|  |  |  |     "factory": (dir0) => new Dir1(dir0), | 
					
						
							|  |  |  |     "parameters": [[Dir0]], | 
					
						
							|  |  |  |     "annotations" : [new Decorator({selector: '[dir1]', bind: {'attr1': 'prop'}})] | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   reflector.registerType(Dir2, { | 
					
						
							|  |  |  |     "factory": (dir1) => new Dir2(dir1), | 
					
						
							|  |  |  |     "parameters": [[Dir1]], | 
					
						
							|  |  |  |     "annotations" : [new Decorator({selector: '[dir2]', bind: {'attr2': 'prop'}})] | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   reflector.registerType(Dir3, { | 
					
						
							|  |  |  |     "factory": (dir2) => new Dir3(dir2), | 
					
						
							|  |  |  |     "parameters": [[Dir2]], | 
					
						
							|  |  |  |     "annotations" : [new Decorator({selector: '[dir3]', bind: {'attr3': 'prop'}})] | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   reflector.registerType(Dir4, { | 
					
						
							|  |  |  |     "factory": (dir3) => new Dir4(dir3), | 
					
						
							|  |  |  |     "parameters": [[Dir3]], | 
					
						
							|  |  |  |     "annotations" : [new Decorator({selector: '[dir4]', bind: {'attr4': 'prop'}})] | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   reflector.registerGetters({ | 
					
						
							| 
									
										
										
										
											2014-12-05 16:26:30 -08:00
										 |  |  |     "inter0": (a) => a.inter0, "inter1": (a) => a.inter1, | 
					
						
							| 
									
										
										
										
											2014-11-17 20:47:05 -08:00
										 |  |  |     "inter2": (a) => a.inter2, "inter3": (a) => a.inter3, "inter4": (a) => a.inter4, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     "value0": (a) => a.value0, "value1": (a) => a.value1, | 
					
						
							|  |  |  |     "value2": (a) => a.value2, "value3": (a) => a.value3, "value4": (a) => a.value4, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     "prop" : (a) => a.prop | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   reflector.registerSetters({ | 
					
						
							|  |  |  |     "inter0": (a,v) => a.inter0 = v, "inter1": (a,v) => a.inter1 = v, | 
					
						
							|  |  |  |     "inter2": (a,v) => a.inter2 = v, "inter3": (a,v) => a.inter3 = v, "inter4": (a,v) => a.inter4 = v, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     "value0": (a,v) => a.value0 = v, "value1": (a,v) => a.value1 = v, | 
					
						
							|  |  |  |     "value2": (a,v) => a.value2 = v, "value3": (a,v) => a.value3 = v, "value4": (a,v) => a.value4 = v, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     "prop": (a,v) => a.prop = v | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-20 12:07:48 -08:00
										 |  |  |   var reader = new CachingDirectiveMetadataReader(); | 
					
						
							| 
									
										
										
										
											2014-12-02 13:21:39 -08:00
										 |  |  |   compiler = new Compiler(null, reader, new Parser(new Lexer()), new CompilerCache()); | 
					
						
							| 
									
										
										
										
											2014-11-20 12:07:48 -08:00
										 |  |  |   annotatedComponent = reader.annotatedType(BenchmarkComponent); | 
					
						
							| 
									
										
										
										
											2014-11-14 14:03:03 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-17 11:01:21 -08:00
										 |  |  | export function main() { | 
					
						
							|  |  |  |   setup(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   benchmark(`Compiler.compile 5*${COUNT} element no bindings`, function() { | 
					
						
							|  |  |  |     var template = loadTemplate('templateNoBindings', COUNT); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     benchmarkStep('run', function() { | 
					
						
							|  |  |  |       // Need to clone every time as the compiler might modify the template!
 | 
					
						
							|  |  |  |       var cloned = DOM.clone(template); | 
					
						
							| 
									
										
										
										
											2014-12-02 13:21:39 -08:00
										 |  |  |       compiler.compileAllLoaded(null, annotatedComponent, cloned); | 
					
						
							| 
									
										
										
										
											2014-11-17 11:01:21 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   benchmark(`Compiler.compile 5*${COUNT} element with bindings`, function() { | 
					
						
							|  |  |  |     var template = loadTemplate('templateWithBindings', COUNT); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     benchmarkStep('run', function() { | 
					
						
							|  |  |  |       // Need to clone every time as the compiler might modify the template!
 | 
					
						
							|  |  |  |       var cloned = DOM.clone(template); | 
					
						
							| 
									
										
										
										
											2014-12-02 13:21:39 -08:00
										 |  |  |       compiler.compileAllLoaded(null, annotatedComponent, cloned); | 
					
						
							| 
									
										
										
										
											2014-11-17 11:01:21 -08:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2014-11-17 20:47:05 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |   benchmark(`instantiate 5*${COUNT} element with bindings`, function() { | 
					
						
							|  |  |  |     var template = loadTemplate('templateWithBindings', COUNT); | 
					
						
							|  |  |  |     var protoView = compiler.compileWithCache(null, annotatedComponent, template); | 
					
						
							| 
									
										
										
										
											2014-12-05 16:26:30 -08:00
										 |  |  |     var rootRecordRange = new ProtoRecordRange().instantiate(null, null); | 
					
						
							| 
									
										
										
										
											2014-11-17 20:47:05 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  |     benchmarkStep('run', function() { | 
					
						
							|  |  |  |       var view = protoView.instantiate(null, null, null); | 
					
						
							|  |  |  |       // also include adding / removing the RecordRange from the parent in the benchmark.
 | 
					
						
							|  |  |  |       rootRecordRange.addRange(view.recordRange); | 
					
						
							|  |  |  |       view.recordRange.remove(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2014-11-14 14:03:03 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-17 11:01:21 -08:00
										 |  |  | function loadTemplate(templateId, repeatCount) { | 
					
						
							|  |  |  |   var template = DOM.querySelectorAll(document, `#${templateId}`)[0]; | 
					
						
							|  |  |  |   var content = DOM.getInnerHTML(template); | 
					
						
							|  |  |  |   var result = ''; | 
					
						
							|  |  |  |   for (var i=0; i<repeatCount; i++) { | 
					
						
							|  |  |  |     result += content; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   return DOM.createTemplate(result); | 
					
						
							| 
									
										
										
										
											2014-11-14 14:03:03 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-17 11:01:21 -08:00
										 |  |  | // Caching reflector as reflection in Dart using Mirrors
 | 
					
						
							| 
									
										
										
										
											2014-11-20 12:07:48 -08:00
										 |  |  | class CachingDirectiveMetadataReader extends DirectiveMetadataReader { | 
					
						
							| 
									
										
										
										
											2014-11-21 21:19:23 -08:00
										 |  |  |   _cache: Map; | 
					
						
							| 
									
										
										
										
											2014-11-17 11:01:21 -08:00
										 |  |  |   constructor() { | 
					
						
							|  |  |  |     this._cache = MapWrapper.create(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   annotatedType(type:Type):AnnotatedType { | 
					
						
							|  |  |  |     var result = MapWrapper.get(this._cache, type); | 
					
						
							|  |  |  |     if (isBlank(result)) { | 
					
						
							|  |  |  |       result = super.annotatedType(type); | 
					
						
							|  |  |  |       MapWrapper.set(this._cache, type, result); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return result; | 
					
						
							|  |  |  |   } | 
					
						
							| 
									
										
										
										
											2014-11-14 14:03:03 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Decorator({ | 
					
						
							|  |  |  |   selector: '[dir0]', | 
					
						
							|  |  |  |   bind: { | 
					
						
							|  |  |  |     'attr0': 'prop' | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }) | 
					
						
							|  |  |  | class Dir0 {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | @Decorator({ | 
					
						
							|  |  |  |   selector: '[dir1]', | 
					
						
							|  |  |  |   bind: { | 
					
						
							|  |  |  |     'attr1': 'prop' | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }) | 
					
						
							| 
									
										
										
										
											2014-11-17 20:47:05 -08:00
										 |  |  | class Dir1 { | 
					
						
							|  |  |  |   constructor(dir0:Dir0) {} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-11-14 14:03:03 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | @Decorator({ | 
					
						
							|  |  |  |   selector: '[dir2]', | 
					
						
							|  |  |  |   bind: { | 
					
						
							|  |  |  |     'attr2': 'prop' | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }) | 
					
						
							| 
									
										
										
										
											2014-11-17 20:47:05 -08:00
										 |  |  | class Dir2 { | 
					
						
							|  |  |  |   constructor(dir1:Dir1) {} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-11-14 14:03:03 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | @Decorator({ | 
					
						
							|  |  |  |   selector: '[dir3]', | 
					
						
							|  |  |  |   bind: { | 
					
						
							|  |  |  |     'attr3': 'prop' | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }) | 
					
						
							| 
									
										
										
										
											2014-11-17 20:47:05 -08:00
										 |  |  | class Dir3 { | 
					
						
							|  |  |  |   constructor(dir2:Dir2) {} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-11-14 14:03:03 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | @Decorator({ | 
					
						
							|  |  |  |   selector: '[dir4]', | 
					
						
							|  |  |  |   bind: { | 
					
						
							|  |  |  |     'attr4': 'prop' | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }) | 
					
						
							| 
									
										
										
										
											2014-11-17 20:47:05 -08:00
										 |  |  | class Dir4 { | 
					
						
							|  |  |  |   constructor(dir3:Dir3) {} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-11-14 14:03:03 -08:00
										 |  |  | 
 | 
					
						
							|  |  |  | @Component({ | 
					
						
							|  |  |  |   template: new TemplateConfig({ | 
					
						
							|  |  |  |     directives: [Dir0, Dir1, Dir2, Dir3, Dir4] | 
					
						
							|  |  |  |   }) | 
					
						
							|  |  |  | }) | 
					
						
							| 
									
										
										
										
											2014-11-17 11:01:21 -08:00
										 |  |  | class BenchmarkComponent {} | 
					
						
							| 
									
										
										
										
											2014-11-14 14:03:03 -08:00
										 |  |  | 
 |