Summary - Performance
- TypeScript course
- Expert
- Lesson 26/35
- 4 min
Write a performance-focused library project.
Summary: performance optimization
Performance optimization has three pillars: reduce type complexity (simplify large unions and deep recursion), find hot spots with --generateTrace, and split the structure with project references to enable incremental builds. Together they save minutes instead of seconds in large TypeScript projects.
tsc --generateTrace ./trace
# find the most expensive type
# simplify the unions
# split with referencesFirst trace, then simplify, last split.
Practice Validate the performance config
Write a validatePerfConfig function that checks a config's incremental and composite fields and pushes to a warnings array when missing. Also add a warning if maxNodeCount is greater than 1,000,000. Print the warning count for the config { incremental: true, composite: true, maxNodeCount: 500000 }.
Starter code
// performans config'ini doğrula
// artımlı ve composite bayraklarını kontrol et
// düğüm sayısı yüksekse uyar
// uyarı sayısını yazdırSample solution
type PerfConfig = {
incremental?: boolean;
composite?: boolean;
maxNodeCount?: number;
};
function validatePerfConfig(config: PerfConfig): string[] {
const warnings: string[] = [];
if (!config.incremental) warnings.push("enable incremental");
if (!config.composite) warnings.push("enable composite for references");
if (config.maxNodeCount && config.maxNodeCount > 1000000) {
warnings.push("maxNodeCount very high");
}
return warnings;
}
const config = { incremental: true, composite: true, maxNodeCount: 500000 };
console.log(validatePerfConfig(config).length);Expected output
0
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is the correct order of performance optimization?
- First simplify, then trace, last split
- First trace, then simplify, last split Correct answer
- First trace, then split, last simplify
- First split, then simplify, last trace
Explanation: First tracing finds the hot spot, then type complexity is simplified, and last the structure is split; simplifying or splitting without knowing the hot spot is blind.
2 Simplification pattern
// app → core → utils
// app değişirse: yalnızca ____ yeniden derlenir
// app → core → utils
// app değişirse: yalnızca app yeniden derlenir
Explanation: If a leaf in the dependency chain changes only that project is rebuilt; utils, core, and all are wrong because dependents are unaffected.
3 Performance parts
| Term | Meaning |
|---|---|
--generateTrace |
finds the hot spot |
Tür sadeleştirme |
reduces complexity |
Project references |
splits the structure |
.tsbuildinfo |
incremental cache |