Skip to content

Commit

Permalink
Skip immutable constants for performance reasons
Browse files Browse the repository at this point in the history
  • Loading branch information
yuchi committed Nov 30, 2018
1 parent 4fffbb6 commit 84b905d
Showing 1 changed file with 31 additions and 7 deletions.
38 changes: 31 additions & 7 deletions src/hooks.macro.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,24 @@ function isFunction(t, path) {
return false;
}

function isImmutableLiteral(t, path) {
if (t.isVariableDeclarator(path)) {
const initPath = path.get('init');

if (
t.isBigIntLiteral(initPath) ||
t.isBooleanLiteral(initPath) ||
t.isNullLiteral(initPath) ||
t.isNumericLiteral(initPath) ||
t.isStringLiteral(initPath)
) {
return true;
}
}

return false;
}

function visitInputsReferences(parentPath, entryPath, babel, visitor) {
const { types: t } = babel;

Expand All @@ -53,15 +71,21 @@ function visitInputsReferences(parentPath, entryPath, babel, visitor) {
return;
}

// Traverse only “constant” function references (as in “never re-assigned”)
if (binding.constant && isFunction(t, binding.path)) {
visitInputsReferences(parentPath, binding.path, babel, visitor);
return;
if (binding.constant) {
// Traverse only “constant” function references (as in “never re-assigned”)
if (isFunction(t, binding.path)) {
visitInputsReferences(parentPath, binding.path, babel, visitor);
return;
}

// Skip known immutables (numbers, booleans), they will never change
if (isImmutableLiteral(t, binding.path)) {
return;
}
}

// All other bindings are included
else {
visitor(path);
}
visitor(path);
},
});
}
Expand Down

0 comments on commit 84b905d

Please sign in to comment.