From c2990d35481be0dba8c54a245e69eb80280af966 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sat, 28 Mar 2015 05:37:06 +1100 Subject: [PATCH] add array cache to es6.destructuring to avoid running toArray multiple times --- .../transformers/es6/destructuring.js | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/src/babel/transformation/transformers/es6/destructuring.js b/src/babel/transformation/transformers/es6/destructuring.js index 400716b8b8..48d7e16c09 100644 --- a/src/babel/transformation/transformers/es6/destructuring.js +++ b/src/babel/transformation/transformers/es6/destructuring.js @@ -231,6 +231,7 @@ class DestructuringTransformer { constructor(opts) { this.blockHoist = opts.blockHoist; this.operator = opts.operator; + this.arrays = {}; this.nodes = opts.nodes || []; this.scope = opts.scope; 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) { // we need to assign the current value of the assignment to avoid evaluating // it more than once @@ -434,11 +443,7 @@ class DestructuringTransformer { // 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 - var toArray = arrayRef; - - if (!this.file.isLoose("es6.destructuring")) { - toArray = this.scope.toArray(arrayRef, count); - } + var toArray = this.toArray(arrayRef, count); if (t.isIdentifier(toArray)) { // we've been given an identifier so it must have been inferred to be an @@ -446,8 +451,8 @@ class DestructuringTransformer { arrayRef = toArray; } else { arrayRef = this.scope.generateUidBasedOnNode(arrayRef); + this.arrays[arrayRef.name] = true; this.nodes.push(this.buildVariableDeclaration(arrayRef, toArray)); - //this.getBinding(arrayRef.name).assignTypeGeneric("Array"); } // @@ -461,11 +466,7 @@ class DestructuringTransformer { var elemRef; if (t.isRestElement(elem)) { - elemRef = arrayRef; - - if (!this.file.isLoose("es6.destructuring")) { - elemRef = this.scope.toArray(arrayRef); - } + elemRef = this.toArray(arrayRef); if (i > 0) { elemRef = t.callExpression(t.memberExpression(elemRef, t.identifier("slice")), [t.literal(i)]);