Performance Optimization Overview
Estimated Time: 3 hours | Difficulty: Advanced | Prerequisites: Change Detection, RxJS
ng build --stats-json with webpack-bundle-analyzer. Premature optimization based on intuition rather than profiling is the number one waste of engineering time in Angular projects. The 80/20 rule applies heavily: OnPush change detection + lazy loading + proper track in @for loops will solve 80% of performance problems in 20% of the effort.
Bundle Size Optimization
Build Analysis
Lazy Loading
Deferrable Views (Angular 17+)
Deferrable views are Angular’s answer to the question “how do I lazy-load something that is not a route?” Before@defer, you had to use dynamic imports with NgComponentOutlet or build custom intersection observer logic. Now the framework handles all of that with declarative syntax. The code for the deferred component is not even included in the initial bundle — it is split into a separate chunk automatically.
Runtime Performance
OnPush Change Detection
Signals for Fine-Grained Reactivity
TrackBy for Lists
Virtual Scrolling
Image Optimization
NgOptimizedImage Directive
Memory Management
Subscription Cleanup
Memory leaks from unsubscribed observables are the silent killer of Angular app performance. Every subscription that outlives its component is a function reference that prevents garbage collection — and each one adds overhead to every change detection cycle. In production, this manifests as the app gradually slowing down the longer a user keeps a tab open.Web Workers for Heavy Computation
Network Optimization
HTTP Caching
Client-side HTTP caching can eliminate redundant network requests entirely. The simplest pattern is a Map-based cache for GET requests. For more sophisticated needs, the “stale-while-revalidate” pattern (shown below) gives users instant responses from cache while silently refreshing data in the background — this is the same strategy that Chrome’s HTTP cache uses and what makes apps feel “snappy.”Core Web Vitals
Performance Checklist
Build Time
- Enable production mode
- AOT compilation
- Tree shaking enabled
- Bundle analysis < 200KB initial
Load Time
- Lazy load routes
- Defer heavy components
- Preload strategies
- Image optimization
Runtime
- OnPush everywhere
- Signals for state
- TrackBy for lists
- Virtual scrolling
Network
- HTTP caching
- Compression (gzip/brotli)
- CDN for assets
- Service Worker caching
Next: Content Projection
Master advanced content projection patterns