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
This commit is contained in:
ole-martin
2019-11-18 21:21:03 +01:00
committed by Nils Knappmeier
parent c819c8b533
commit 23d58e79bb
2 changed files with 38 additions and 1 deletions
+11 -1
View File
@@ -124,6 +124,15 @@ export function template(templateSpec, env) {
}
return value;
},
mergeIfNeeded: function(param, common) {
let obj = param || common;
if (param && common && (param !== common)) {
obj = Utils.extend({}, common, param);
}
return obj;
},
// An empty object to use as replacement for null-contexts
nullContext: Object.seal({}),
@@ -161,7 +170,8 @@ export function template(templateSpec, env) {
container.helpers = Utils.extend({}, env.helpers, options.helpers);
if (templateSpec.usePartial) {
container.partials = Utils.extend({}, env.partials, options.partials);
// Use mergeIfNeeded here to prevent compiling global partials multiple times
container.partials = container.mergeIfNeeded(options.partials, env.partials);
}
if (templateSpec.usePartial || templateSpec.useDecorators) {
container.decorators = Utils.extend({}, env.decorators, options.decorators);
+27
View File
@@ -334,4 +334,31 @@ describe('Regressions', function() {
shouldCompileTo('{{helpa length="foo"}}', [obj, helpers], 'foo');
});
describe('GH-1598: Performance degradation for partials since v4.3.0', function() {
// Do not run test for runs without compiler
if (!Handlebars.compile) {
return;
}
var newHandlebarsInstance;
beforeEach(function() {
newHandlebarsInstance = Handlebars.create();
});
afterEach(function() {
sinon.restore();
});
it('should only compile global partials once', function() {
var templateSpy = sinon.spy(newHandlebarsInstance, 'template');
newHandlebarsInstance.registerPartial({
'dude': 'I am a partial'
});
var string = 'Dudes: {{> dude}} {{> dude}}';
newHandlebarsInstance.compile(string)(); // This should compile template + partial once
newHandlebarsInstance.compile(string)(); // This should only compile template
equal(templateSpy.callCount, 3);
sinon.restore();
});
});
});