@@ -192,6 +192,8 @@ export class PathRecognizer {
192192 specificity : number ;
193193 terminal : boolean = true ;
194194 hash : string ;
195+ private cache : Map < string , ComponentInstruction > = new Map < string , ComponentInstruction > ( ) ;
196+
195197
196198 // TODO: cache component instruction instances by params and by ParsedUrl instance
197199
@@ -252,23 +254,26 @@ export class PathRecognizer {
252254
253255 var auxiliary ;
254256 var instruction : ComponentInstruction ;
257+ var urlParams ;
258+ var allParams ;
255259 if ( isPresent ( currentSegment ) ) {
256260 // If this is the root component, read query params. Otherwise, read matrix params.
257261 var paramsSegment = beginningSegment instanceof RootUrl ? beginningSegment : currentSegment ;
258262
259- var allParams = isPresent ( paramsSegment . params ) ?
260- StringMapWrapper . merge ( paramsSegment . params , positionalParams ) :
261- positionalParams ;
263+ allParams = isPresent ( paramsSegment . params ) ?
264+ StringMapWrapper . merge ( paramsSegment . params , positionalParams ) :
265+ positionalParams ;
262266
263- var urlParams = serializeParams ( paramsSegment . params ) ;
267+ urlParams = serializeParams ( paramsSegment . params ) ;
264268
265- instruction = new ComponentInstruction ( urlPath , urlParams , this , allParams ) ;
266269
267270 auxiliary = currentSegment . auxiliary ;
268271 } else {
269- instruction = new ComponentInstruction ( urlPath , [ ] , this , positionalParams ) ;
272+ allParams = positionalParams ;
270273 auxiliary = [ ] ;
274+ urlParams = [ ] ;
271275 }
276+ instruction = this . _getInstruction ( urlPath , urlParams , this , allParams ) ;
272277 return new PathMatch ( instruction , nextSegment , auxiliary ) ;
273278 }
274279
@@ -289,6 +294,18 @@ export class PathRecognizer {
289294 var nonPositionalParams = paramTokens . getUnused ( ) ;
290295 var urlParams = serializeParams ( nonPositionalParams ) ;
291296
292- return new ComponentInstruction ( urlPath , urlParams , this , params ) ;
297+ return this . _getInstruction ( urlPath , urlParams , this , params ) ;
298+ }
299+
300+ private _getInstruction ( urlPath : string , urlParams : List < string > , _recognizer : PathRecognizer ,
301+ params : StringMap < string , any > ) : ComponentInstruction {
302+ var hashKey = urlPath + '?' + urlParams . join ( '?' ) ;
303+ if ( this . cache . has ( hashKey ) ) {
304+ return this . cache . get ( hashKey ) ;
305+ }
306+ var instruction = new ComponentInstruction ( urlPath , urlParams , _recognizer , params ) ;
307+ this . cache . set ( hashKey , instruction ) ;
308+
309+ return instruction ;
293310 }
294311}
0 commit comments