Skip to content

Commit

Permalink
remove object keys check to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
vigneshshanmugam committed Jan 5, 2018
1 parent 4a27059 commit 5a9b411
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = t => {
const consequent = path.get("consequent");
const alternate = path.get("alternate");

const EX = Symbol("Expression");
const { Expression: EX } = h.typeSymbols(t);

// Convention:
// ===============
Expand Down
30 changes: 27 additions & 3 deletions packages/babel-plugin-minify-simplify/src/helpers.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
"use strict";

// Types as Symbols - for comparing types
const types = {};
// This is a test key which is used to avoid Object.keys check
// Object.keys() check is really expensive
// https://gist.github.com/vigneshshanmugam/c766550ecd02292dcdfbf0bf013b9d3d
const testKey = "Expression";

const typeSymbols = t => {
// don't recompute
if (types[testKey] !== undefined) {
return types;
}
t.TYPES.forEach(type => {
types[type] = Symbol.for(type);
});
return types;
};

const VOID_0 = t => t.unaryExpression("void", t.numericLiteral(0), true);

const isExpression = (t, node, typeSymbol) =>
typeof typeSymbol !== "symbol" ? false : t.isExpression(node);
const isNodeOfType = (t, node, typeSymbol) =>
typeof typeSymbol !== "symbol"
? false
: t["is" + Symbol.keyFor(typeSymbol)](node);

const isPatternMatchesPath = t =>
function _isPatternMatchesPath(patternValue, inputPath) {
Expand All @@ -18,13 +38,17 @@ const isPatternMatchesPath = t =>
if (typeof patternValue === "function") {
return patternValue(inputPath);
}
if (isExpression(t, inputPath.node, patternValue)) return true;
if (isNodeOfType(t, inputPath.node, patternValue)) return true;
const evalResult = inputPath.evaluate();
if (!evalResult.confident || !inputPath.isPure()) return false;
return evalResult.value === patternValue;
};

module.exports = {
// Types as Symbols
typeSymbols,
// This is required for resolving type aliases
isNodeOfType,
VOID_0,
isPatternMatchesPath
};
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module.exports = t => {
return evalResult.confident && input.isPure() && !evalResult.value;
};

const EX = Symbol("Expression");
const { Expression: EX } = h.typeSymbols(t);

// Convention:
// [left, operator, right, handler(leftNode, rightNode)]
Expand Down

0 comments on commit 5a9b411

Please sign in to comment.