add array cache to es6.destructuring to avoid running toArray multiple times

This commit is contained in:
Sebastian McKenzie 2015-03-28 05:37:06 +11:00
parent 803f8a3bbc
commit c2990d3548

View File

@ -231,6 +231,7 @@ class DestructuringTransformer {
constructor(opts) { constructor(opts) {
this.blockHoist = opts.blockHoist; this.blockHoist = opts.blockHoist;
this.operator = opts.operator; this.operator = opts.operator;
this.arrays = {};
this.nodes = opts.nodes || []; this.nodes = opts.nodes || [];
this.scope = opts.scope; this.scope = opts.scope;
this.file = opts.file; this.file = opts.file;
@ -276,6 +277,14 @@ class DestructuringTransformer {
} }
} }
toArray(node, count) {
if (this.file.isLoose("es6.destructuring") || (t.isIdentifier(node) && this.arrays[node.name])) {
return node;
} else {
return this.scope.toArray(node, count);
}
}
pushAssignmentPattern(pattern, valueRef) { pushAssignmentPattern(pattern, valueRef) {
// we need to assign the current value of the assignment to avoid evaluating // we need to assign the current value of the assignment to avoid evaluating
// it more than once // it more than once
@ -434,11 +443,7 @@ class DestructuringTransformer {
// return a locally bound identifier if it's been inferred to be an array, // return a locally bound identifier if it's been inferred to be an array,
// otherwise it'll be a call to a helper that will ensure it's one // otherwise it'll be a call to a helper that will ensure it's one
var toArray = arrayRef; var toArray = this.toArray(arrayRef, count);
if (!this.file.isLoose("es6.destructuring")) {
toArray = this.scope.toArray(arrayRef, count);
}
if (t.isIdentifier(toArray)) { if (t.isIdentifier(toArray)) {
// we've been given an identifier so it must have been inferred to be an // we've been given an identifier so it must have been inferred to be an
@ -446,8 +451,8 @@ class DestructuringTransformer {
arrayRef = toArray; arrayRef = toArray;
} else { } else {
arrayRef = this.scope.generateUidBasedOnNode(arrayRef); arrayRef = this.scope.generateUidBasedOnNode(arrayRef);
this.arrays[arrayRef.name] = true;
this.nodes.push(this.buildVariableDeclaration(arrayRef, toArray)); this.nodes.push(this.buildVariableDeclaration(arrayRef, toArray));
//this.getBinding(arrayRef.name).assignTypeGeneric("Array");
} }
// //
@ -461,11 +466,7 @@ class DestructuringTransformer {
var elemRef; var elemRef;
if (t.isRestElement(elem)) { if (t.isRestElement(elem)) {
elemRef = arrayRef; elemRef = this.toArray(arrayRef);
if (!this.file.isLoose("es6.destructuring")) {
elemRef = this.scope.toArray(arrayRef);
}
if (i > 0) { if (i > 0) {
elemRef = t.callExpression(t.memberExpression(elemRef, t.identifier("slice")), [t.literal(i)]); elemRef = t.callExpression(t.memberExpression(elemRef, t.identifier("slice")), [t.literal(i)]);