Skip to content

Relating large unions of literal types performs poorly #53191

Closed
@ahejlsberg

Description

@ahejlsberg

πŸ•— Version & Regression Information

All versions.

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type Digits = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9';
type T9XXXX = `9${Digits}${Digits}${Digits}${Digits}`;
type TXXXXX = `${Exclude<Digits, '0'>}${Digits}${Digits}${Digits}${Digits}`;

let x: Exclude<TXXXXX, T9XXXX> = '88888';

πŸ™ Actual behavior

The example above takes ~45s to compile on a reasonably fast machine. The underlying issue is that as we relate large unions of primitive types, we have a fast path for matches, but not for mismatches. Because the majority of the constituents of TXXXXX are not present in T9XXXX, we spend a lot of time in the slow path that attempts to find matches by iterating through all constituents.

πŸ™‚ Expected behavior

We can do much better by implementing a fast path for mismatches.

Activity

self-assigned this
on Mar 10, 2023
added this to the TypeScript 5.1.0 milestone on Mar 10, 2023
fatcerberus

fatcerberus commented on Mar 10, 2023

@fatcerberus

The example above takes ~45s to compile on a reasonably fast machine.

So more out of morbid curiosity than anything...

fatcerberus@Bruces-MacBook-Pro temp % time npx tsc wtf.ts
npx tsc wtf.ts  60.97s user 0.17s system 101% cpu 1:00.28 total
fatcerberus@Bruces-MacBook-Pro temp % node -v
v19.7.0

That was with an M2 Pro. Holy crap.

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

Metadata

Metadata

Assignees

Labels

Domain: PerformanceReports of unusually slow behaviorFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

    Development

    Participants

    @fatcerberus@ahejlsberg@RyanCavanaugh@typescript-bot

    Issue actions

      Relating large unions of literal types performs poorly Β· Issue #53191 Β· microsoft/TypeScript