File tree 2 files changed +38
-1
lines changed
2 files changed +38
-1
lines changed Original file line number Diff line number Diff line change @@ -124,6 +124,15 @@ export function template(templateSpec, env) {
124
124
}
125
125
return value ;
126
126
} ,
127
+ mergeIfNeeded : function ( param , common ) {
128
+ let obj = param || common ;
129
+
130
+ if ( param && common && ( param !== common ) ) {
131
+ obj = Utils . extend ( { } , common , param ) ;
132
+ }
133
+
134
+ return obj ;
135
+ } ,
127
136
// An empty object to use as replacement for null-contexts
128
137
nullContext : Object . seal ( { } ) ,
129
138
@@ -161,7 +170,8 @@ export function template(templateSpec, env) {
161
170
container . helpers = Utils . extend ( { } , env . helpers , options . helpers ) ;
162
171
163
172
if ( templateSpec . usePartial ) {
164
- container . partials = Utils . extend ( { } , env . partials , options . partials ) ;
173
+ // Use mergeIfNeeded here to prevent compiling global partials multiple times
174
+ container . partials = container . mergeIfNeeded ( options . partials , env . partials ) ;
165
175
}
166
176
if ( templateSpec . usePartial || templateSpec . useDecorators ) {
167
177
container . decorators = Utils . extend ( { } , env . decorators , options . decorators ) ;
Original file line number Diff line number Diff line change @@ -334,4 +334,31 @@ describe('Regressions', function() {
334
334
335
335
shouldCompileTo ( '{{helpa length="foo"}}' , [ obj , helpers ] , 'foo' ) ;
336
336
} ) ;
337
+
338
+ describe ( 'GH-1598: Performance degradation for partials since v4.3.0' , function ( ) {
339
+ // Do not run test for runs without compiler
340
+ if ( ! Handlebars . compile ) {
341
+ return ;
342
+ }
343
+
344
+ var newHandlebarsInstance ;
345
+ beforeEach ( function ( ) {
346
+ newHandlebarsInstance = Handlebars . create ( ) ;
347
+ } ) ;
348
+ afterEach ( function ( ) {
349
+ sinon . restore ( ) ;
350
+ } ) ;
351
+
352
+ it ( 'should only compile global partials once' , function ( ) {
353
+ var templateSpy = sinon . spy ( newHandlebarsInstance , 'template' ) ;
354
+ newHandlebarsInstance . registerPartial ( {
355
+ 'dude' : 'I am a partial'
356
+ } ) ;
357
+ var string = 'Dudes: {{> dude}} {{> dude}}' ;
358
+ newHandlebarsInstance . compile ( string ) ( ) ; // This should compile template + partial once
359
+ newHandlebarsInstance . compile ( string ) ( ) ; // This should only compile template
360
+ equal ( templateSpy . callCount , 3 ) ;
361
+ sinon . restore ( ) ;
362
+ } ) ;
363
+ } ) ;
337
364
} ) ;
You can’t perform that action at this time.
0 commit comments