Skip to content

Commit

Permalink
Improve rendering performance
Browse files Browse the repository at this point in the history
Avoid unnecessary copies via Utils.extend in hot paths.
  • Loading branch information
mohd-akram committed Sep 1, 2024
1 parent 7de4b41 commit 93a7125
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 42 deletions.
3 changes: 2 additions & 1 deletion lib/handlebars/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ export function moveHelperToHooks(instance, helperName, keepHelper) {
if (instance.helpers[helperName]) {
instance.hooks[helperName] = instance.helpers[helperName];
if (!keepHelper) {
delete instance.helpers[helperName];
// Using delete is slow
instance.helpers[helperName] = undefined;
}
}
}
11 changes: 0 additions & 11 deletions lib/handlebars/internal/create-new-lookup-object.js

This file was deleted.

32 changes: 15 additions & 17 deletions lib/handlebars/internal/proto-access.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,30 @@
import { createNewLookupObject } from './create-new-lookup-object';
import { extend } from '../utils';
import logger from '../logger';

const loggedProperties = Object.create(null);

export function createProtoAccessControl(runtimeOptions) {
let defaultMethodWhiteList = Object.create(null);
defaultMethodWhiteList['constructor'] = false;
defaultMethodWhiteList['__defineGetter__'] = false;
defaultMethodWhiteList['__defineSetter__'] = false;
defaultMethodWhiteList['__lookupGetter__'] = false;

let defaultPropertyWhiteList = Object.create(null);
// Create an object with "null"-prototype to avoid truthy results on
// prototype properties.
const propertyWhiteList = Object.create(null);
// eslint-disable-next-line no-proto
defaultPropertyWhiteList['__proto__'] = false;
propertyWhiteList['__proto__'] = false;
extend(propertyWhiteList, runtimeOptions.allowedProtoProperties);

const methodWhiteList = Object.create(null);
methodWhiteList['constructor'] = false;
methodWhiteList['__defineGetter__'] = false;
methodWhiteList['__defineSetter__'] = false;
methodWhiteList['__lookupGetter__'] = false;
extend(methodWhiteList, runtimeOptions.allowedProtoMethods);

return {
properties: {
whitelist: createNewLookupObject(
defaultPropertyWhiteList,
runtimeOptions.allowedProtoProperties
),
whitelist: propertyWhiteList,
defaultValue: runtimeOptions.allowProtoPropertiesByDefault
},
methods: {
whitelist: createNewLookupObject(
defaultMethodWhiteList,
runtimeOptions.allowedProtoMethods
),
whitelist: methodWhiteList,
defaultValue: runtimeOptions.allowProtoMethodsByDefault
}
};
Expand Down
20 changes: 7 additions & 13 deletions lib/handlebars/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,18 @@ export function template(templateSpec, env) {
}
partial = env.VM.resolvePartial.call(this, partial, context, options);

let extendedOptions = Utils.extend({}, options, {
hooks: this.hooks,
protoAccessControl: this.protoAccessControl
});

let result = env.VM.invokePartial.call(
this,
partial,
context,
extendedOptions
);
options.hooks = this.hooks;
options.protoAccessControl = this.protoAccessControl;

let result = env.VM.invokePartial.call(this, partial, context, options);

if (result == null && env.compile) {
options.partials[options.name] = env.compile(
partial,
templateSpec.compilerOptions,
env
);
result = options.partials[options.name](context, extendedOptions);
result = options.partials[options.name](context, options);
}
if (result != null) {
if (options.indent) {
Expand Down Expand Up @@ -438,6 +431,7 @@ function wrapHelpersToPassLookupProperty(mergedHelpers, container) {
function passLookupPropertyOption(helper, container) {
const lookupProperty = container.lookupProperty;
return wrapHelper(helper, options => {
return Utils.extend({ lookupProperty }, options);
options.lookupProperty = lookupProperty;
return options;
});
}

0 comments on commit 93a7125

Please sign in to comment.