Lesson 7 of 9 · 24m
Compiler Performance: Measure Before You Optimize
Use TypeScript's built-in diagnostics to distinguish parse, bind, check, and emit costs, then fix the actual bottleneck rather than cargo-culting flags.
TypeScript performance advice on the internet is a museum of half-remembered fixes: disable sourcemaps, enable skipLibCheck, buy a faster laptop, split the project, delete interface and use type. Sometimes those help. Often they miss the actual cost. The compiler can tell you where time goes. Senior engineers start there.
Get the baseline report
tsc --noEmit --diagnosticsYou will see counters such as files, lines, types, instantiations, memory, and phase timings:
Files: 1842 Types: 118823 Instantiations: 402118
Parse time: 0.84s Bind time: 0.21s Check time: 11.72s
Memory used: 512310K Total time: 12.77sIf check time dominates, configuration micro-tuning will not save you. If parse time dominates in a huge repository, file inclusion and dependency count matter more than one clever type rewrite.
Generate a performance trace
tsc --noEmit --generateTrace trace-dir writes compiler trace events. Inspect them when generated clients or schema types make one innocent-looking import cost seconds.
Type complexity has a shape
The notorious offenders are not mysterious:
type DeepPartial<T> = {
[K in keyof T]?: T[K] extends object ? DeepPartial<T[K]> : T[K]
}Recursive conditional types, huge unions, distributive mapped types, inference-heavy generics, and intersections of giant object models multiply work. Sometimes the fix is a simpler public type; sometimes it is isolating the expensive type behind a narrow boundary.
Measure inclusion before rewriting types
A surprisingly common failure is compiling far more than intended:
{"include": ["src"], "exclude": ["src/**/*.test.ts", "src/generated/tmp"]}tsc --noEmit --listFiles | wc -l
tsc --noEmit --explainFiles | lessexplainFiles answers "why is this file in my program?" Generated fixtures, test utilities, and broad type inclusion can double the workload.
Treat speed flags as tradeoffs, not upgrades
skipLibCheck: true is often pragmatic because dependency declarations are outside your control, but it can hide incompatibilities in the library graph. isolatedModules does not speed checking; it constrains emit-safe syntax. Project references can help but add graph complexity. There is no free performance dial, only informed tradeoffs.
Field notes
- Record compiler version and command with every benchmark. TypeScript performance characteristics change across releases.
- High instantiation counts are often a better smell than raw file count when check time explodes.
- Editor slowness and CLI slowness overlap but are not identical; measure both if developers are complaining.
- Generated code should be small, stable, and isolated. Large generated unions belong behind narrow interfaces.
- Do not cache performance conclusions forever. Re-measure after dependency and compiler upgrades.
Was this lesson helpful?