babel/packages/babel-plugin-transform-block-scoping
Sophie Alpert 6a7223af29 Fix O(n^2) getLetReferences – 40% faster on large flat files
`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):

```
<!DOCTYPE html>
<script src="https://unpkg.com/babel-standalone@7.0.0-beta.3/babel.js"></script>
<script type="text/babel" src="https://unpkg.com/react-dom@16.2.0/umd/react-dom.development.js"></script>
```
2017-12-14 21:55:26 -08:00
..
2017-12-14 16:47:27 -05:00

@babel/plugin-transform-block-scoping

Compile ES2015 block scoping (const and let) to ES5

Examples

In

{
  let a = 3
}

let a = 3

Out

{
  var _a = 3;
}

var a = 3;

Installation

npm install --save-dev @babel/plugin-transform-block-scoping

Usage

.babelrc

Without options:

{
  "plugins": ["@babel/plugin-transform-block-scoping"]
}

With options:

{
  "plugins": [
    ["@babel/plugin-transform-block-scoping", {
      "throwIfClosureRequired": true
    }]
  ]
}

Via CLI

babel --plugins @babel/plugin-transform-block-scoping script.js

Via Node API

require("@babel/core").transform("code", {
  plugins: ["@babel/plugin-transform-block-scoping"]
});

Options throwIfClosureRequired

In cases such as the following it's impossible to rewrite let/const without adding an additional function and closure while transforming:

for (let i = 0; i < 5; i++) {
  setTimeout(() => console.log(i), 1);
}

In extremely performance-sensitive code, this can be undesirable. If "throwIfClosureRequired": true is set, Babel throws when transforming these patterns instead of automatically adding an additional function.