Skip to content

improve type checking speed - #18623

Merged
nicolo-ribaudo merged 5 commits into
mainfrom
jlhwung/perf-type-check
Nov 16, 2026
Merged

nicolo-ribaudo merged 5 commits into
mainfrom
jlhwung/perf-type-check

Conversation

@JLHwung

@JLHwung JLHwung commented Nov 14, 2026 •

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

In this PR we implement a few typing refactors to improve the make tscheck performance, the overall walltime (make tscheck) has been improved from 65s to 49s. This PR can be reviewed by commits.

Checking that a generic type argument satisfies `t.Node` compares it
with the union of all node types, which is slow when the argument is a
conditional type such as `Extract<...>` or `NonNullable<...>`:

- `NodePath`'s `P` default (`NonNullable<t.ParentMaps[...]>`)
- `VisitNode<S, ToNode<K>>` in `VisitorProp`
- `NodePath<Extract<N, { type: T }>>` in the internal `setType` helper

Bind the first two with `infer U extends t.Node`, which gives the same
types for every instantiation, and narrow the path union directly in
`setType`. This saves about 2s of `@babel/traverse`'s type check.

Assisted-by: Claude Opus 5.5
- `cloneNode` passed `node` narrowed to `NonNullable<T>` to
  `cloneNodeInternal`, so `T` was inferred as `NonNullable<T>` and
  checked against `t.Node | undefined | null`. Pass `<T>` explicitly.
- The type predicates of `is()` were checked against `t.Node` while
  `Extract<t.Node, { type: T }>` was still a union of conditional types.
  Bind it with `infer N extends t.Node`, which gives the same type for
  every `T`.

This saves about 1.3s of `@babel/types`'s type check.

Assisted-by: Claude Opus 5.5
Declaration emit printed some inferred types in full, and every project
that depends on these packages parses the result:

- `mixinPlugins` in `@babel/parser` inlined the class returned by each
  plugin (`plugin-utils.d.ts`: 537 KB -> 0.8 KB).
- `getCachedPaths`, `replaceInline`, `replaceExpressionWithStatements`
  and `replaceWithSourceString` in `@babel/traverse` inlined the union
  of every `NodePath` (`cache.d.ts`: 268 KB -> 0.6 KB,
  `replacement.d.ts`: 324 KB -> 1.9 KB).

The annotations are the types TypeScript inferred.

Assisted-by: Claude Opus 5.5
`options.ts` took the `Plugin` alias from `plugin-utils.ts`, so the
declarations of `@babel/parser`'s public options loaded `plugin-utils.d.ts`
and through it the declarations of every parser plugin. Projects that
only use the options type no longer parse the `estree` and `v8intrinsic`
plugin declarations (about 97 KB).

Assisted-by: Claude Opus 5.5
@JLHwung JLHwung added the PR: Internal 🏠 A type of pull request used for our changelog categories label Nov 14, 2026
@babel-bot

babel-bot commented Nov 14, 2026 •

Copy link
Copy Markdown
Collaborator

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

@pkg-pr-new

pkg-pr-new Bot commented Nov 14, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

commit: 93dcd93

@JLHwung
JLHwung marked this pull request as ready for review November 14, 2026 18:59

@nicolo-ribaudo nicolo-ribaudo left a comment

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.

nice

@nicolo-ribaudo
nicolo-ribaudo added this pull request to the merge queue Nov 16, 2026
Merged via the queue into main with commit 16a2d67 Nov 16, 2026
46 checks passed
@nicolo-ribaudo
nicolo-ribaudo deleted the jlhwung/perf-type-check branch November 16, 2026 11:24
@babel-bot

Copy link
Copy Markdown
Collaborator

Backported to 7.x in #18670.

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

Labels

PR: Internal 🏠 A type of pull request used for our changelog categories

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants