Performance Optimization
Golden Rule
Measure before and after. Without data, you're guessing. Profile, optimize, re-profile.
Process
- Define the goal — e.g. "API responds in <200ms p95", "bundle <200KB"
- Measure the baseline — use profiling tools, not intuition
- Identify the bottleneck — is it CPU, I/O, memory, network, database?
- Optimize the bottleneck — the slowest part determines overall speed
- Re-measure — verify improvement, check for regressions
- Repeat — the next bottleneck is now visible
Common Bottlenecks
- Database: N+1 queries, missing indexes, full table scans
- Network: serial requests (batch them!), large payloads, no compression
- Rendering: unnecessary re-renders, large lists without virtualization
- Memory: leaks, excessive allocations, no caching
- CPU: inefficient algorithms, tight loops with I/O
Anti-Patterns
- Optimizing before profiling
- Micro-optimizing (saving 1ms in a function called 5 times)
- Caching without invalidation strategy
- Premature optimization (Knuth: "the root of all evil")