From b1a7338ea7b45fb6d346962f03655a1321bc92ca Mon Sep 17 00:00:00 2001 From: Mohamed Akram Date: Sun, 1 Sep 2024 17:48:23 +0400 Subject: [PATCH] Improve rendering performance Avoid unnecessary copies via Utils.extend in hot paths. --- .../internal/create-new-lookup-object.js | 11 ----------- lib/handlebars/internal/proto-access.js | 8 +++++--- lib/handlebars/runtime.js | 17 +++++------------ 3 files changed, 10 insertions(+), 26 deletions(-) delete mode 100644 lib/handlebars/internal/create-new-lookup-object.js diff --git a/lib/handlebars/internal/create-new-lookup-object.js b/lib/handlebars/internal/create-new-lookup-object.js deleted file mode 100644 index 256f1eca1..000000000 --- a/lib/handlebars/internal/create-new-lookup-object.js +++ /dev/null @@ -1,11 +0,0 @@ -import { extend } from '../utils'; - -/** - * Create a new object with "null"-prototype to avoid truthy results on prototype properties. - * The resulting object can be used with "object[property]" to check if a property exists - * @param {...object} sources a varargs parameter of source objects that will be merged - * @returns {object} - */ -export function createNewLookupObject(...sources) { - return extend(Object.create(null), ...sources); -} diff --git a/lib/handlebars/internal/proto-access.js b/lib/handlebars/internal/proto-access.js index 424e0a714..c9f1ee9a7 100644 --- a/lib/handlebars/internal/proto-access.js +++ b/lib/handlebars/internal/proto-access.js @@ -1,9 +1,11 @@ -import { createNewLookupObject } from './create-new-lookup-object'; +import { extend } from '../utils'; import logger from '../logger'; const loggedProperties = Object.create(null); export function createProtoAccessControl(runtimeOptions) { + // Create an object with "null"-prototype to avoid truthy results on + // prototype properties. let defaultMethodWhiteList = Object.create(null); defaultMethodWhiteList['constructor'] = false; defaultMethodWhiteList['__defineGetter__'] = false; @@ -16,14 +18,14 @@ export function createProtoAccessControl(runtimeOptions) { return { properties: { - whitelist: createNewLookupObject( + whitelist: extend( defaultPropertyWhiteList, runtimeOptions.allowedProtoProperties ), defaultValue: runtimeOptions.allowProtoPropertiesByDefault }, methods: { - whitelist: createNewLookupObject( + whitelist: extend( defaultMethodWhiteList, runtimeOptions.allowedProtoMethods ), diff --git a/lib/handlebars/runtime.js b/lib/handlebars/runtime.js index b9aec1e3b..3c758f25e 100644 --- a/lib/handlebars/runtime.js +++ b/lib/handlebars/runtime.js @@ -74,17 +74,10 @@ 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( @@ -92,7 +85,7 @@ export function template(templateSpec, env) { templateSpec.compilerOptions, env ); - result = options.partials[options.name](context, extendedOptions); + result = options.partials[options.name](context, options); } if (result != null) { if (options.indent) {