Skip to content

Commit

Permalink
fix(runtime.js): partials compile not caching (#1600)
Browse files Browse the repository at this point in the history
Reintroduce "merge" function, no called "mergeIfNeeded", that only creates a new partials
object if both "env.partials" and "options.partials" are set.

closes #1598
  • Loading branch information
ole-martin authored and nknapp committed Nov 18, 2019
1 parent c819c8b commit 23d58e7
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
12 changes: 11 additions & 1 deletion lib/handlebars/runtime.js
Expand Up @@ -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({}),

Expand Down Expand Up @@ -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);
Expand Down
27 changes: 27 additions & 0 deletions spec/regressions.js
Expand Up @@ -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();
});
});
});

0 comments on commit 23d58e7

Please sign in to comment.