optional for-of transformer that puts arrays in fast path
This commit is contained in:
parent
b08f2061b5
commit
393d52088a
13
lib/6to5/transformation/templates/for-of-fast.js
Normal file
13
lib/6to5/transformation/templates/for-of-fast.js
Normal file
@ -0,0 +1,13 @@
|
||||
for (var LOOP_OBJECT = OBJECT,
|
||||
IS_ARRAY = Array.isArray(LOOP_OBJECT),
|
||||
INDEX = 0,
|
||||
LOOP_OBJECT = IS_ARRAY ? LOOP_OBJECT : LOOP_OBJECT[Symbol.iterator]();;) {
|
||||
if (IS_ARRAY) {
|
||||
if (INDEX >= LOOP_OBJECT.length) break;
|
||||
ID = LOOP_OBJECT[INDEX++];
|
||||
} else {
|
||||
INDEX = LOOP_OBJECT.next();
|
||||
if (INDEX.done) break;
|
||||
ID = INDEX.value;
|
||||
}
|
||||
}
|
||||
@ -68,6 +68,7 @@ _.each({
|
||||
computedPropertyNames: require("./transformers/es6-computed-property-names"),
|
||||
destructuring: require("./transformers/es6-destructuring"),
|
||||
defaultParameters: require("./transformers/es6-default-parameters"),
|
||||
forOfFast: require("./transformers/optional-for-of-fast"),
|
||||
forOf: require("./transformers/es6-for-of"),
|
||||
unicodeRegex: require("./transformers/es6-unicode-regex"),
|
||||
abstractReferences: require("./transformers/es7-abstract-references"),
|
||||
|
||||
39
lib/6to5/transformation/transformers/optional-for-of-fast.js
Normal file
39
lib/6to5/transformation/transformers/optional-for-of-fast.js
Normal file
@ -0,0 +1,39 @@
|
||||
var util = require("../../util");
|
||||
var t = require("../../types");
|
||||
|
||||
exports.optional = true;
|
||||
|
||||
exports.ForOfStatement = function (node, parent, file, scope) {
|
||||
var left = node.left;
|
||||
var declar, id;
|
||||
|
||||
if (t.isIdentifier(left)) {
|
||||
id = left;
|
||||
} else if (t.isVariableDeclaration(left)) {
|
||||
id = left.declarations[0].id;
|
||||
declar = t.variableDeclaration(left.kind, [
|
||||
t.variableDeclarator(id)
|
||||
]);
|
||||
} else {
|
||||
throw file.errorWithNode(left, "Unknown node type " + left.type + " in ForOfStatement");
|
||||
}
|
||||
|
||||
var node2 = util.template("for-of-fast", {
|
||||
LOOP_OBJECT: file.generateUidIdentifier("loopObject", scope),
|
||||
IS_ARRAY: file.generateUidIdentifier("isArray", scope),
|
||||
INDEX: file.generateUidIdentifier("i", scope),
|
||||
ID: id,
|
||||
OBJECT: node.right
|
||||
});
|
||||
|
||||
t.inheritsComments(node2, node);
|
||||
t.ensureBlock(node);
|
||||
|
||||
var block = node2.body;
|
||||
if (declar) {
|
||||
block.body.unshift(declar);
|
||||
}
|
||||
block.body = block.body.concat(node.body.body);
|
||||
|
||||
return node2;
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user