From 7a1f79f564559482dd9e801976fa3ad76a7e3f79 Mon Sep 17 00:00:00 2001 From: Vignesh Shanmugam Date: Thu, 28 Dec 2017 11:45:51 +0100 Subject: [PATCH 1/2] removing symbol logic for performance --- .../src/conditional-expression.js | 2 +- .../src/helpers.js | 27 +++---------------- .../src/logical-expression.js | 2 +- 3 files changed, 5 insertions(+), 26 deletions(-) diff --git a/packages/babel-plugin-minify-simplify/src/conditional-expression.js b/packages/babel-plugin-minify-simplify/src/conditional-expression.js index 6bcc92a35..152e531a2 100644 --- a/packages/babel-plugin-minify-simplify/src/conditional-expression.js +++ b/packages/babel-plugin-minify-simplify/src/conditional-expression.js @@ -13,7 +13,7 @@ module.exports = t => { const consequent = path.get("consequent"); const alternate = path.get("alternate"); - const { Expression: EX } = h.typeSymbols(t); + const EX = Symbol("Expression"); // Convention: // =============== diff --git a/packages/babel-plugin-minify-simplify/src/helpers.js b/packages/babel-plugin-minify-simplify/src/helpers.js index a3bad07b4..f2919a507 100644 --- a/packages/babel-plugin-minify-simplify/src/helpers.js +++ b/packages/babel-plugin-minify-simplify/src/helpers.js @@ -2,25 +2,8 @@ const VOID_0 = t => t.unaryExpression("void", t.numericLiteral(0), true); -// Types as Symbols - for comparing types -// init must be empty object - -// computing this involves checking object.keys() to be of length 0 -// skipped otherwise -const types = {}; -const typeSymbols = t => { - // don't recompute - if (Object.keys(types).length < 1) { - t.TYPES.forEach(type => { - types[type] = Symbol.for(type); - }); - } - return types; -}; - -const isNodeOfType = (t, node, typeSymbol) => - typeof typeSymbol !== "symbol" - ? false - : t["is" + Symbol.keyFor(typeSymbol)](node); +const isExpression = (t, node, typeSymbol) => + typeof typeSymbol !== "symbol" ? false : t.isExpression(node); const isPatternMatchesPath = t => function _isPatternMatchesPath(patternValue, inputPath) { @@ -35,7 +18,7 @@ const isPatternMatchesPath = t => if (typeof patternValue === "function") { return patternValue(inputPath); } - if (isNodeOfType(t, inputPath.node, patternValue)) return true; + if (isExpression(t, inputPath.node, patternValue)) return true; const evalResult = inputPath.evaluate(); if (!evalResult.confident || !inputPath.isPure()) return false; return evalResult.value === patternValue; @@ -43,9 +26,5 @@ const isPatternMatchesPath = t => module.exports = { VOID_0, - // Types as Symbols - typeSymbols, - // This is required for resolving type aliases - isNodeOfType, isPatternMatchesPath }; diff --git a/packages/babel-plugin-minify-simplify/src/logical-expression.js b/packages/babel-plugin-minify-simplify/src/logical-expression.js index 448d08e87..8d2f56c2f 100644 --- a/packages/babel-plugin-minify-simplify/src/logical-expression.js +++ b/packages/babel-plugin-minify-simplify/src/logical-expression.js @@ -40,7 +40,7 @@ module.exports = t => { return evalResult.confident && input.isPure() && !evalResult.value; }; - const { Expression: EX } = h.typeSymbols(t); + const EX = Symbol("Expression"); // Convention: // [left, operator, right, handler(leftNode, rightNode)] From eaa217ad44dce55478434ecf6f0753e19ca9639c Mon Sep 17 00:00:00 2001 From: Vignesh Shanmugam Date: Fri, 5 Jan 2018 23:24:26 +0100 Subject: [PATCH 2/2] remove object keys check to improve performance --- .../src/conditional-expression.js | 2 +- .../src/helpers.js | 30 +++++++++++++++++-- .../src/logical-expression.js | 2 +- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/packages/babel-plugin-minify-simplify/src/conditional-expression.js b/packages/babel-plugin-minify-simplify/src/conditional-expression.js index 152e531a2..6bcc92a35 100644 --- a/packages/babel-plugin-minify-simplify/src/conditional-expression.js +++ b/packages/babel-plugin-minify-simplify/src/conditional-expression.js @@ -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: // =============== diff --git a/packages/babel-plugin-minify-simplify/src/helpers.js b/packages/babel-plugin-minify-simplify/src/helpers.js index f2919a507..1fa8e6269 100644 --- a/packages/babel-plugin-minify-simplify/src/helpers.js +++ b/packages/babel-plugin-minify-simplify/src/helpers.js @@ -2,8 +2,28 @@ const VOID_0 = t => t.unaryExpression("void", t.numericLiteral(0), true); -const isExpression = (t, node, typeSymbol) => - typeof typeSymbol !== "symbol" ? false : t.isExpression(node); +// 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 isNodeOfType = (t, node, typeSymbol) => + typeof typeSymbol !== "symbol" + ? false + : t["is" + Symbol.keyFor(typeSymbol)](node); const isPatternMatchesPath = t => function _isPatternMatchesPath(patternValue, inputPath) { @@ -18,7 +38,7 @@ 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; @@ -26,5 +46,9 @@ const isPatternMatchesPath = t => module.exports = { VOID_0, + // Types as Symbols + typeSymbols, + // This is required for resolving type aliases + isNodeOfType, isPatternMatchesPath }; diff --git a/packages/babel-plugin-minify-simplify/src/logical-expression.js b/packages/babel-plugin-minify-simplify/src/logical-expression.js index 8d2f56c2f..448d08e87 100644 --- a/packages/babel-plugin-minify-simplify/src/logical-expression.js +++ b/packages/babel-plugin-minify-simplify/src/logical-expression.js @@ -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)]