From eb80fd76e6c97d3c2902a422cb760e1263e9e18f Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Sun, 10 Dec 2023 23:29:45 +0000 Subject: [PATCH] Avoid object property lookup [perf] --- lib/serialize/blocks.js | 20 +++++++++++--------- lib/serialize/functions.js | 2 +- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/lib/serialize/blocks.js b/lib/serialize/blocks.js index e525c682..e95003a6 100644 --- a/lib/serialize/blocks.js +++ b/lib/serialize/blocks.js @@ -190,15 +190,17 @@ module.exports = { let numInternalOnlyFunctions = 0; if (numLocalFunctions > 0) { const internalFunctions = new Set(); - for (const {name: paramName, fnDef, fnRecords} of params) { + for (const param of params) { + const {fnDef} = param; if (!fnDef) continue; // Record param name containing this function in function def object - (fnDef.internalScopeParamNames || (fnDef.internalScopeParamNames = [])).push(paramName); + (fnDef.internalScopeParams || (fnDef.internalScopeParams = [])).push(param); // Delete values for this param from all scopes. // Delete the dependencies created between scope record and value record for all scopes. - for (const fnRecord of fnRecords) { + const paramName = param.name; + for (const fnRecord of param.fnRecords) { const {scope} = fnRecord; scope.values[paramName] = undefined; @@ -381,7 +383,7 @@ module.exports = { for (const blockFunction of blockFunctions) { const { scopes: fnScopes, externalVars, internalVars, globalVarNames: fnGlobalVarNames, - containsEval: fnContainsEval, isStrict, internalScopeParamNames, isScopeInternalOnly + containsEval: fnContainsEval, isStrict, internalScopeParams, isScopeInternalOnly } = blockFunction; if (!isScopeInternalOnly) { @@ -425,7 +427,7 @@ module.exports = { // Replace placeholder var with reference to scope var const fnNodeWithWrapper = fnRecord.node, wrapperParentNode = getNodeWithinWrapperParent(fnNodeWithWrapper); - if (!internalScopeParamNames) { + if (!internalScopeParams) { // Replace placeholder var with reference to scope var if (wrapperParentNode) { wrapperParentNode[0] = fnNode; @@ -532,13 +534,13 @@ module.exports = { } // If param is a locally-defined function, add `x = ...` to node - if (internalScopeParamNames) { + if (internalScopeParams) { // If unnamed function, wrap in `(0, ...)` to prevent implicit naming if (!node.id) node = t.sequenceExpression([t.numericLiteral(0), node]); - for (const paramName of internalScopeParamNames) { - const varNode = t.identifier(paramName); - paramsByName[paramName].localVarNodes.push(varNode); + for (const param of internalScopeParams) { + const varNode = t.identifier(param.name); + param.localVarNodes.push(varNode); node = t.assignmentExpression('=', varNode, node); } } diff --git a/lib/serialize/functions.js b/lib/serialize/functions.js index dddb1c5b..fb7d3efa 100644 --- a/lib/serialize/functions.js +++ b/lib/serialize/functions.js @@ -109,7 +109,7 @@ module.exports = { fnDef.scopes = new Map(); // Keyed by scope object, value of each is function instance's record fnDef.virtualBlock = undefined; - fnDef.internalScopeParamNames = undefined; // May be set in `processBlock()` + fnDef.internalScopeParams = undefined; // May be set in `processBlock()` fnDef.isScopeInternalOnly = false; // May be set in `processBlock()` functions.set(fnId, fnDef);