Skip to content

Commit

Permalink
Avoid object property lookup [perf]
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Dec 10, 2023
1 parent 7f21945 commit eb80fd7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
20 changes: 11 additions & 9 deletions lib/serialize/blocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/serialize/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit eb80fd7

Please sign in to comment.