Skip to content

Slow typings related to large enumsΒ #53761

Closed
@Spacetech

Description

@Spacetech

Bug Report

πŸ”Ž Search Terms

Slow, enums, large. Related issue that looks similar: #42824

πŸ•— Version & Regression Information

Not sure when this started. I'm testing on 5.0.4

  • This is a performance problem

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

The code is in the playground link. The exact tsconfig.json that I'm testing with is:

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "commonjs",
    "declaration": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true,
    "skipLibCheck": true
  }
}

πŸ™ Actual behavior / πŸ™‚ Expected behavior

The code in the playground link has an enum containing 753 values and contains the following ITest interface:

interface ITest {
    transforms?: PartialRecord<ItemType, ItemType>;
}

where PartialRecord is defined at the top of the file as:

export type PartialRecord<K extends number, V> = { [key in K]?: V };

Running a trace build / analyze results in:

PS Z:\Data\Downloads\test> tsc --generateTrace tstrace && npx analyze-trace tstrace --forceMillis 100
Hot Spots
└─ Check file z:\data\downloads\test\index.ts (259ms)
   └─ Check variable declaration from (line 777, char 11) to (line 777, char 61) (189ms)
      └─ Check expression from (line 777, char 20) to (line 777, char 61) (186ms)

The hotspot is line 777 - const target = items[itemType1]?.transforms?.[itemType2];

image

Now if I change the ITest interface to the following:

interface ITest {
    transforms?: { [key in ItemType]?: ItemType };
}

(I'm essentially using the same typing that PartialRecord<ItemType, ItemType> provides)

The performance is improved now, it's 2.5x faster. The self-time for the expression on line 777 went from 171ms to 66ms -

PS Z:\Data\Downloads\test> tsc --generateTrace tstrace && npx analyze-trace tstrace --forceMillis 100
Hot Spots
└─ Check file z:\data\downloads\test\index.ts (153ms)

image

My codebase has a lot of large enums, so this matters quite a bit to us in the overall scheme of things.

Is it possible to speed up the typing in the case when we use something like PartialRecord with large enums?

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: PerformanceReports of unusually slow behaviorFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions