From 5baa36109e15929e6f6b823af82e5a20403d2b71 Mon Sep 17 00:00:00 2001 From: Benedikt Meurer Date: Sun, 29 Oct 2017 02:16:04 +0200 Subject: [PATCH] Fix access to "-1" property on nodesOut array. (#6582) Similar to the fixes in https://github.com/babel/babel/pull/6580 and https://github.com/babel/babel/pull/6581, accesses of the form ```js nodesOut[nodesOut.length - 1] ``` where `nodesOut` can be an empty array, are bad for performance in Node. In this particular case it's easy to restructure the code a bit to not require the array access at all, but just track the current `tail` as we go. This is a non-breaking performance fix. --- .../src/index.js | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/packages/babel-plugin-transform-es2015-destructuring/src/index.js b/packages/babel-plugin-transform-es2015-destructuring/src/index.js index 0930e860fb..169d99311e 100644 --- a/packages/babel-plugin-transform-es2015-destructuring/src/index.js +++ b/packages/babel-plugin-transform-es2015-destructuring/src/index.js @@ -519,20 +519,17 @@ export default function({ types: t }) { } } + let tail = null; const nodesOut = []; for (const node of nodes) { - const tail = nodesOut[nodesOut.length - 1]; - if ( - tail && - t.isVariableDeclaration(tail) && - t.isVariableDeclaration(node) - ) { + if (tail !== null && t.isVariableDeclaration(node)) { // Create a single compound declarations tail.declarations.push(...node.declarations); } else { // Make sure the original node kind is used for each compound declaration node.kind = nodeKind; nodesOut.push(node); + tail = t.isVariableDeclaration(node) ? node : null; } }