Description
🔎 Search Terms
"optional chaining", "slow compilation", "performance"
🕗 Version & Regression Information
- Reproducible on 5.7.3 and nightly Version 5.9.0-dev.20250225
- Trying different versions on the playground it appears that the behavior changed between 4.6.4 and 4.7.4
⏯ Playground Link
💻 Code
type Nested = {
children: Nested[],
value: number
}
const search : Nested = {
children: [],
value: 5
}
const a = search.children?.findIndex((n) => n.value == 1);
const b = search.children[a]?.children?.findIndex((n) => n.value == 2);
const c = search.children[a]?.children[b]?.children?.findIndex((n) => n.value == 3);
const d = search.children[a]?.children[b]?.children[c]?.children.findIndex((n) => n.value == 4);
const e = search.children[a]?.children[b]?.children[c]?.children[d]?.children.findIndex((n) => n.value == 5);
const f = search.children[a]?.children[b]?.children[c]?.children[d]?.children[e]?.children.findIndex((n) => n.value == 6);
const g = search.children[a]?.children[b]?.children[c]?.children[d]?.children[e]?.children[f]?.children.findIndex((n) => n.value == 7);
console.log('Done');
🙁 Actual behavior
Compilation time gets increasingly slow as more nested accesses using optional chaining are added.
Timing on my machine:
- 5 chains: < 1s
- 6 chains: 8s
- 7 chains: 220s
🙂 Expected behavior
Compilation time remains about the same or increases linearly.
Additional information about the issue
Originally found while diagnosing slow compile times in a project dealing with searches through a potentially nested API response.
Output of tsc --diagnostics
:
Files: 58
Lines: 10638
Identifiers: 8659
Symbols: 7911
Types: 3124
Instantiations: 1150
Memory used: 56818K
I/O read: 0.02s
I/O write: 0.00s
Parse time: 0.12s
Bind time: 0.04s
Check time: 165.76s
Emit time: 56.28s
Total time: 222.20s
As a workaround, refactoring to assign the repeated index lookups to intermediate variables resolves the performance issue.