Skip to content

perf: improve loadConfig performance - #18249

Open
JLHwung wants to merge 23 commits into
babel:mainfrom
JLHwung:perf-load-config
Open

JLHwung wants to merge 23 commits into
babel:mainfrom
JLHwung:perf-load-config

Conversation

@JLHwung

@JLHwung JLHwung commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor
Q                       A
Fixed Issues?
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

In this PR we added a new benchmark for the babelCore.loadOptionsSync API and optimized hot paths guided by the CPU profile created by node --cpu-prof.

Here are the benchmark results

# a minimal `{ targets: "defaults" }` Babel config:
$ node --predictable benchmark/babel-core/load-config-target-defaults/bench.js
baseline: 44_244 ops/sec ±3.9% (0.023ms)
current: 66_984 ops/sec ±3.95% (0.015ms)

$ node --predictable benchmark/babel-core/load-config-target-defaults-preset-env-typescript/bench.js
baseline: 21_239 ops/sec ±3.67% (0.047ms)
current: 26_876 ops/sec ±3.85% (0.037ms)

$ node --predictable benchmark/babel-core/load-config-assumptions-target-defaults-preset-env-plugin-typescript-decorators/bench.js
baseline: 21_110 ops/sec ±5.01% (0.047ms)
current: 28_287 ops/sec ±4.04% (0.035ms)

# without `--predictable` flag
$ find ./benchmark/babel-core/load-config-* -name bench.js -print -exec node {} \;
./benchmark/babel-core/load-config-assumptions-target-defaults-preset-env-plugin-typescript-decorators/bench.js
baseline: 25_903 ops/sec ±0.7% (0.039ms)
current: 32_645 ops/sec ±0.52% (0.031ms)
./benchmark/babel-core/load-config-target-defaults/bench.js
baseline: 54_553 ops/sec ±0.69% (0.018ms)
current: 79_866 ops/sec ±0.6% (0.013ms)
./benchmark/babel-core/load-config-target-defaults-preset-env-typescript/bench.js
baseline: 23_677 ops/sec ±0.55% (0.042ms)
current: 29_701 ops/sec ±0.87% (0.034ms)

This PR can be reviewed by commits. I plan to add new benchmarks and open new PRs once I have satisfying results.

@JLHwung JLHwung added the PR: Performance 🏃‍♀️ A type of pull request used for our changelog categories label Sep 21, 2026
@babel-bot

babel-bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Collaborator

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

@pkg-pr-new

pkg-pr-new Bot commented Sep 22, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

commit: 3bca981

Comment on lines +87 to +89
"You appear to be using a native ECMAScript module configuration " +
"file, which is only supported when running Babel asynchronously " +
"or when using the Node.js `--experimental-require-module` flag.",

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

(unrelated to this PR) Now that we only support Node.js versions with require(esm) support, we should revise this error.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I feel like maybe we can just remove this error message and simplify the loading process as this branch should be unreachable, since Babel 8 requires Node.js versions with require(esm). I will leave it to another PR.

@JLHwung

This comment was marked as outdated.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Config resolution, cache invalidation, and source-map alias handling introduce regressions that must be corrected.

Get a fresh assessment by requesting another Copilot review.

Review effort: Balanced
Findings: 2 Medium severity

Open (2)
What changed in this PR

Optimizes Babel configuration loading and adds benchmarks measuring the improved hot paths.

Changes:

  • Caches target resolution and configuration-module loading.
  • Reduces wrapper allocation and configuration-processing overhead.
  • Adds three load-config benchmarks and fixes an existing preset benchmark.
File Description
yarn.lock Locks the new flru dependency.
packages/​babel-core/​test/​errors-stacks.js Updates expected stack traces.
packages/​babel-core/​src/​transform.ts Reuses wrapped gensync handlers.
packages/​babel-core/​src/​transform-ast.ts Reuses wrapped AST transform handlers.
packages/​babel-core/​src/​parse.ts Reuses wrapped parser handlers.
packages/​babel-core/​src/​errors/​rewrite-stack-trace.ts Adds gensync stack-wrapper support.
packages/​babel-core/​src/​config/​resolve-targets.ts Caches string target resolution.
packages/​babel-core/​src/​config/​partial.ts Avoids unnecessary path and environment work.
packages/​babel-core/​src/​config/​index.ts Streamlines public config runners.
packages/​babel-core/​src/​config/​full.ts Reduces generator-wrapper overhead.
packages/​babel-core/​src/​config/​files/​module-types.ts Reuses the wrapped require function.
packages/​babel-core/​src/​config/​files/​configuration.ts Caches module loading and optimizes path handling.
packages/​babel-core/​src/​config/​config-chain.ts Optimizes environment and option processing.
packages/​babel-core/​package.json Adds flru.
benchmark/​babel-core/​real-case-preset-env-typescript/​babel-parser-tokenizer-index.mjs Corrects presets configuration.
benchmark/​babel-core/​load-config-target-defaults/​bench.js Benchmarks minimal config loading.
benchmark/​babel-core/​load-config-target-defaults/​babel.config.cjs Defines the minimal benchmark config.
benchmark/​babel-core/​load-config-target-defaults-preset-env-typescript/​bench.js Benchmarks preset-heavy loading.
benchmark/​babel-core/​load-config-target-defaults-preset-env-typescript/​babel.config.cjs Defines the preset benchmark config.
benchmark/​babel-core/​load-config-assumptions-target-defaults-preset-env-plugin-typescript-decorators/​bench.js Benchmarks a complex configuration.
benchmark/​babel-core/​load-config-assumptions-target-defaults-preset-env-plugin-typescript-decorators/​babel.config.cjs Defines the complex benchmark config.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Extensionless configuration specifiers are misclassified, and the two-argument createConfigItem callback overload passes an undefined callback.

Get a fresh assessment by requesting another Copilot review.

Review effort: Balanced
Findings: 1 High severity

Open (1)
Resolved since last review (2)
Previously missed (1)

In code that hasn't changed since last review

Medium severity Select loader using resolved filepath extension

packages/​babel-core/​src/​config/​files/​configuration.ts:341

Use the resolved filepath to select the loader. A valid extensionless specifier such as configFile: "example-config" resolves to .../index.js, but extName remains empty, so this now sends the JavaScript module to readConfigJSON5 and fails to load it. The same applies to relative extensionless paths resolved by Node.

Comment thread packages/babel-core/src/config/index.ts Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Extensionless JavaScript configuration requests are incorrectly routed to the JSON5 parser.

Review effort: Balanced
Findings: 1 High severity

Open (1)
Resolved since last review (1)

: require.resolve(name, { paths: [dirname] });

const conf = yield* readConfig(filepath, envName, caller);
const conf = yield* readConfig(filepath, extName, envName, caller);

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

@nicolo-ribaudo

Copy link
Copy Markdown
Member

It seems like this was closed by accident.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: core 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.

5 participants