Skip to content

Commit 23d58e7

Browse files
ole-martinnknapp
authored andcommittedNov 18, 2019
fix(runtime.js): partials compile not caching (#1600)
Reintroduce "merge" function, no called "mergeIfNeeded", that only creates a new partials object if both "env.partials" and "options.partials" are set. closes #1598
1 parent c819c8b commit 23d58e7

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed
 

Diff for: ‎lib/handlebars/runtime.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ export function template(templateSpec, env) {
124124
}
125125
return value;
126126
},
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+
},
127136
// An empty object to use as replacement for null-contexts
128137
nullContext: Object.seal({}),
129138

@@ -161,7 +170,8 @@ export function template(templateSpec, env) {
161170
container.helpers = Utils.extend({}, env.helpers, options.helpers);
162171

163172
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);
165175
}
166176
if (templateSpec.usePartial || templateSpec.useDecorators) {
167177
container.decorators = Utils.extend({}, env.decorators, options.decorators);

Diff for: ‎spec/regressions.js

+27
Original file line numberDiff line numberDiff line change
@@ -334,4 +334,31 @@ describe('Regressions', function() {
334334

335335
shouldCompileTo('{{helpa length="foo"}}', [obj, helpers], 'foo');
336336
});
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+
});
337364
});

0 commit comments

Comments
 (0)
Please sign in to comment.