optimise common typeof cases in es6.spec.symbols transformer

This commit is contained in:
Sebastian McKenzie 2015-07-31 01:37:50 +01:00
parent e95e836d5a
commit 23cf77746d
5 changed files with 13 additions and 1 deletions

View File

@ -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()) {

View File

@ -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 = ["-", "/", "*", "**", "&", "|", ">>", ">>>", "<<", "^"];

View File

@ -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";

View File

@ -1,2 +1,3 @@
var s = Symbol("s");
assert.equal(typeof s, "symbol");
assert.ok(typeof s === "symbol");

View File

@ -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";