From 23cf77746da1554a3b78e3e6c949289c7b74c247 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Fri, 31 Jul 2015 01:37:50 +0100 Subject: [PATCH] optimise common typeof cases in es6.spec.symbols transformer --- .../src/transformation/transformers/es6/spec.symbols.js | 6 ++++++ packages/babel/src/types/index.js | 3 ++- .../transformation/es6.spec.symbols/typeof/actual.js | 2 ++ .../fixtures/transformation/es6.spec.symbols/typeof/exec.js | 1 + .../transformation/es6.spec.symbols/typeof/expected.js | 2 ++ 5 files changed, 13 insertions(+), 1 deletion(-) diff --git a/packages/babel/src/transformation/transformers/es6/spec.symbols.js b/packages/babel/src/transformation/transformers/es6/spec.symbols.js index 455fc4ac7e..80eaf22788 100644 --- a/packages/babel/src/transformation/transformers/es6/spec.symbols.js +++ b/packages/babel/src/transformation/transformers/es6/spec.symbols.js @@ -17,6 +17,12 @@ export var visitor = { UnaryExpression(node, parent, scope, file) { if (node._ignoreSpecSymbols) return; + if (this.parentPath.isBinaryExpression() && t.EQUALITY_BINARY_OPERATORS.indexOf(parent.operator) >= 0) { + // optimise `typeof foo === "string"` since we can determine that they'll never need to handle symbols + var opposite = this.getOpposite(); + if (opposite.isLiteral() && opposite.node.value !== "symbol" && opposite.node.value !== "object") return; + } + if (node.operator === "typeof") { var call = t.callExpression(file.addHelper("typeof"), [node.argument]); if (this.get("argument").isIdentifier()) { diff --git a/packages/babel/src/types/index.js b/packages/babel/src/types/index.js index dce4e78b1a..8975faea2f 100644 --- a/packages/babel/src/types/index.js +++ b/packages/babel/src/types/index.js @@ -39,7 +39,8 @@ export const INHERIT_KEYS = { }; export const BOOLEAN_NUMBER_BINARY_OPERATORS = [">", "<", ">=", "<="]; -export const COMPARISON_BINARY_OPERATORS = ["==", "===", "!=", "!==", "in", "instanceof"]; +export const EQUALITY_BINARY_OPERATORS = ["==", "===", "!=", "!=="]; +export const COMPARISON_BINARY_OPERATORS = EQUALITY_BINARY_OPERATORS.concat(["in", "instanceof"]); export const BOOLEAN_BINARY_OPERATORS = [].concat(COMPARISON_BINARY_OPERATORS, BOOLEAN_NUMBER_BINARY_OPERATORS); export const NUMBER_BINARY_OPERATORS = ["-", "/", "*", "**", "&", "|", ">>", ">>>", "<<", "^"]; diff --git a/packages/babel/test/fixtures/transformation/es6.spec.symbols/typeof/actual.js b/packages/babel/test/fixtures/transformation/es6.spec.symbols/typeof/actual.js index d31f30ca86..872484bd9c 100644 --- a/packages/babel/test/fixtures/transformation/es6.spec.symbols/typeof/actual.js +++ b/packages/babel/test/fixtures/transformation/es6.spec.symbols/typeof/actual.js @@ -1,3 +1,5 @@ var s = Symbol("s"); +assert.ok(typeof s === "symbol"); assert.equal(typeof s, "symbol"); assert.equal(typeof typeof s.foo, "symbol"); +typeof s === "string"; diff --git a/packages/babel/test/fixtures/transformation/es6.spec.symbols/typeof/exec.js b/packages/babel/test/fixtures/transformation/es6.spec.symbols/typeof/exec.js index 20422ebc64..0c53760815 100644 --- a/packages/babel/test/fixtures/transformation/es6.spec.symbols/typeof/exec.js +++ b/packages/babel/test/fixtures/transformation/es6.spec.symbols/typeof/exec.js @@ -1,2 +1,3 @@ var s = Symbol("s"); assert.equal(typeof s, "symbol"); +assert.ok(typeof s === "symbol"); diff --git a/packages/babel/test/fixtures/transformation/es6.spec.symbols/typeof/expected.js b/packages/babel/test/fixtures/transformation/es6.spec.symbols/typeof/expected.js index 80d011bc00..fc73784fe4 100644 --- a/packages/babel/test/fixtures/transformation/es6.spec.symbols/typeof/expected.js +++ b/packages/babel/test/fixtures/transformation/es6.spec.symbols/typeof/expected.js @@ -1,5 +1,7 @@ "use strict"; var s = Symbol("s"); +assert.ok((typeof s === "undefined" ? "undefined" : babelHelpers._typeof(s)) === "symbol"); assert.equal(typeof s === "undefined" ? "undefined" : babelHelpers._typeof(s), "symbol"); assert.equal(babelHelpers._typeof(babelHelpers._typeof(s.foo)), "symbol"); +typeof s === "string";