From 6a7223af29e9dd51017eaaa88932d8a846a91409 Mon Sep 17 00:00:00 2001 From: Sophie Alpert Date: Thu, 14 Dec 2017 21:46:15 -0800 Subject: [PATCH] =?UTF-8?q?Fix=20O(n^2)=20getLetReferences=20=E2=80=93=204?= =?UTF-8?q?0%=20faster=20on=20large=20flat=20files?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `this.blockPath.get("body")` constructs an array of paths corresponding to each node in `blocks.body` so takes O(n) time if n is that length. We were re-constructing that array on each iteration, so the entire loop was O(n^2). On files with many statements in a single block (such as Rollup-generated bundles), this takes a large portion of time. In particular, this makes transforming react-dom.development.js about 40% faster. Not that you should be transforming our bundle with Babel. Test Plan: Make an HTML file with these three lines and watch it in the Chrome Performance tab to see timings (on my machine: 2.9s before, 1.6s after): ``` ``` --- .../babel-plugin-transform-block-scoping/src/index.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/babel-plugin-transform-block-scoping/src/index.js b/packages/babel-plugin-transform-block-scoping/src/index.js index 3b14632af8..1cf3214192 100644 --- a/packages/babel-plugin-transform-block-scoping/src/index.js +++ b/packages/babel-plugin-transform-block-scoping/src/index.js @@ -647,20 +647,20 @@ class BlockScoping { // if (block.body) { + const declarPaths = this.blockPath.get("body"); for (let i = 0; i < block.body.length; i++) { - const declarPath = this.blockPath.get("body")[i]; - addDeclarationsFromChild(declarPath); + addDeclarationsFromChild(declarPaths[i]); } } if (block.cases) { + const declarPaths = this.blockPath.get("cases"); for (let i = 0; i < block.cases.length; i++) { const consequents = block.cases[i].consequent; for (let j = 0; j < consequents.length; j++) { - const declarPath = this.blockPath.get("cases")[i]; const declar = consequents[j]; - addDeclarationsFromChild(declarPath, declar); + addDeclarationsFromChild(declarPaths[i], declar); } } }