Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: lazy load preset-env plugins #13076

Merged
merged 1 commit into from Mar 30, 2021

Conversation

JLHwung
Copy link
Contributor

@JLHwung JLHwung commented Mar 30, 2021

Q                       A
Fixed Issues? Improve preset-env loading on modern targets
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

This commit is drafted by a codemod. On modern targets, most plugins in preset-env are not actually enabled. However, when Babel is loading available-plugins, these plugins are still imported. This PR improves loading speed by lazy loading all the available plugins. They are now loaded only when getPlugin is called.

The cost of resolving many small files and their node_modules is tremendous: In the benchmark case, loading unused plugins costs 663ms. In other words, this PR improving loading preset-env from 1.17s to 477ms when compiling against Node.js 15.

Benchmark setup:

Create a babel.config.json with only targets:

{ "targets": { "node": 15 } }

Run V8 CPU profiler separately on Babel repo

// compiles `./packages/babel-preset-env/src/available-plugins.js` with `preset-env` in node_modules
node --predictable --cpu-prof ./node_modules/.bin/babel --config-file ./babel.config.json --presets @babel/preset-env -- ./packages/babel-preset-env/src/available-plugins.js

// compiles `./packages/babel-preset-env/src/available-plugins.js` with `preset-env` in this PR
node --predictable --cpu-prof ./node_modules/.bin/babel --config-file ./babel.config.json --presets ./packages/babel-preset-env -- ./packages/babel-preset-env/src/available-plugins.js

Load the CPU profile in Chrome DevTools. Check the time of the second LoadFullConfig call

Before:
image

After:
image

@JLHwung JLHwung added pkg: preset-env PR: Performance 🏃‍♀️ A type of pull request used for our changelog categories labels Mar 30, 2021
@codesandbox-ci
Copy link

codesandbox-ci bot commented Mar 30, 2021

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 36de510:

Sandbox Source
babel-repl-custom-plugin Configuration
babel-plugin-multi-config Configuration

@babel-bot
Copy link
Collaborator

babel-bot commented Mar 30, 2021

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/44884/

babel.config.js Outdated Show resolved Hide resolved
@JLHwung JLHwung force-pushed the lazy-load-preset-env-plugins branch from e9ef29c to 36de510 Compare March 30, 2021 16:02
Copy link
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Copy link
Member

@existentialism existentialism left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🏃

@nicolo-ribaudo nicolo-ribaudo merged commit 3e174e9 into babel:main Mar 30, 2021
@nicolo-ribaudo nicolo-ribaudo deleted the lazy-load-preset-env-plugins branch March 30, 2021 19:43
@github-actions github-actions bot added the outdated A closed issue/PR that is archived due to age. Recommended to make a new issue label Jun 30, 2021
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jun 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
outdated A closed issue/PR that is archived due to age. Recommended to make a new issue pkg: preset-env PR: Performance 🏃‍♀️ A type of pull request used for our changelog categories
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants