@@ -15,12 +15,17 @@ import {ViewPort} from './viewport';
1515import { Content } from './shadow_dom_emulation/content_tag' ;
1616import { LightDom , DestinationLightDom } from './shadow_dom_emulation/light_dom' ;
1717import { ShadowDomStrategy } from './shadow_dom_strategy' ;
18+ import { ViewPool } from './view_pool' ;
1819
1920const NG_BINDING_CLASS = 'ng-binding' ;
2021const NG_BINDING_CLASS_SELECTOR = '.ng-binding' ;
2122// TODO(tbosch): Cannot use `const` because of Dart.
2223var NO_FORMATTERS = MapWrapper . create ( ) ;
2324
25+ // TODO(rado): make this configurable/smarter.
26+ var VIEW_POOL_CAPACITY = 10000 ;
27+ var VIEW_POOL_PREFILL = 0 ;
28+
2429/**
2530 * Const of making objects: http://jsperf.com/instantiate-size-of-object
2631 */
@@ -268,6 +273,7 @@ export class ProtoView {
268273 rootBindingOffset:int ;
269274 isTemplateElement:boolean ;
270275 shadowDomStrategy: ShadowDomStrategy ;
276+ _viewPool: ViewPool ;
271277 constructor (
272278 template :Element ,
273279 protoChangeDetector :ProtoChangeDetector ,
@@ -284,10 +290,23 @@ export class ProtoView {
284290 ? 1 : 0 ;
285291 this . isTemplateElement = this . element instanceof TemplateElement ;
286292 this . shadowDomStrategy = shadowDomStrategy ;
293+ this . _viewPool = new ViewPool ( VIEW_POOL_CAPACITY ) ;
287294 }
288295
289296 // TODO(rado): hostElementInjector should be moved to hydrate phase.
290297 instantiate ( hostElementInjector : ElementInjector ) :View {
298+ if ( this . _viewPool . length ( ) == 0 ) this . _preFillPool ( hostElementInjector ) ;
299+ var view = this . _viewPool . pop ( ) ;
300+ return isPresent ( view ) ? view : this . _instantiate ( hostElementInjector ) ;
301+ }
302+
303+ _preFillPool ( hostElementInjector : ElementInjector ) {
304+ for ( var i = 0 ; i < VIEW_POOL_PREFILL ; i ++ ) {
305+ this . _viewPool . push ( this . _instantiate ( hostElementInjector ) ) ;
306+ }
307+ }
308+
309+ _instantiate ( hostElementInjector : ElementInjector ) : View {
291310 var rootElementClone = this . instantiateInPlace ? this . element : DOM . clone ( this . element ) ;
292311 var elementsWithBindingsDynamic ;
293312 if ( this . isTemplateElement ) {
@@ -409,6 +428,10 @@ export class ProtoView {
409428 return view ;
410429 }
411430
431+ returnToPool ( view : View ) {
432+ this . _viewPool . push ( view ) ;
433+ }
434+
412435 static _addNativeEventListener ( element : Element , eventName : string , expr : AST , view : View ) {
413436 var locals = MapWrapper . create ( ) ;
414437 var innerCallback = ProtoView . buildInnerCallback ( expr , view , locals ) ;
0 commit comments