Lab 09: Performance Types

Time: 60 minutes | Level: Architect | Docker: node:20-alpine

Overview

TypeScript performance engineering: identifying and fixing slow type patterns, satisfies for inference without widening, const type parameters (TS 5.0), NoInfer<T> (TS 5.4), variadic tuple inference, and tsc --diagnostics profiling.


Step 1: tsc --diagnostics Profiling

# Run type checker with performance metrics
tsc --noEmit --diagnostics

# Output:
# Files:                         523
# Lines of Library:            57647
# Lines of Definitions:        42831
# Lines of TypeScript:          8234
# Lines of JavaScript:             0
# Lines of JSON:                 125
# Nodes:                      520931
# Identifiers:                189412
# Symbols:                    148392
# Types:                      108763
# Instantiations:            2847291   ← HIGH: slow types here
# Memory used:               256392K
# Assignability cache size:    27183
# Identity cache size:          5521
# Subtype cache size:            843
# I/O Read time:              0.03s
# Parse time:                 1.42s
# ResolveModule time:         0.18s
# ResolveTypeReference time:  0.02s
# Bind time:                  0.34s
# Check time:                 8.74s   ← Total type check time
# Emit time:                  0.00s
# Total time:                10.75s

# Find the slowest files:
tsc --noEmit --extendedDiagnostics 2>&1 | grep "Check time" | sort -k3 -n

Step 2: Slow Pattern — Large Union Discrimination


Step 3: Slow Pattern — Deep Recursive Conditional Types


Step 4: satisfies — Validate Without Widening


Step 5: const Type Parameters (TypeScript 5.0)


Step 6: Variadic Tuple Types


Step 7: Type-Level Performance Checklist


Step 8: Capstone — FizzBuzz + Type Check Verification

📸 Verified Output:


Summary

Issue
Symptom
Fix

Large union

Slow narrowing

Mapped type + index

Deep recursive

Instantiation count

Limit depth parameter

Wide inference

Lost literal types

satisfies or const T

Slow lib check

Slow total time

skipLibCheck: true

Repeated types

High memory use

Extract to interface

Template literals

Slow with large union

Branded types instead

No cache

Full rebuild every time

incremental: true

Last updated