Angular Performance Optimization
# CHAPTER 24
Angular Performance Optimization
1. Chapter Introduction
A beautiful Angular app that runs slowly will lose users. Performance optimization in Angular involves reducing the amount of work the browser does — fewer DOM updates, smaller bundle sizes, deferred module loading. This chapter covers the essential techniques used by professional Angular teams.2. Learning Objectives
-
Implement
OnPushchange detection strategy.
-
Use
trackBywith*ngForto avoid unnecessary DOM re-renders.
- Implement lazy loading for modules and images.
-
Use the
asyncpipe to prevent manual subscriptions.
- Understand virtual scrolling for large lists.
3. Change Detection: Default vs OnPush
By default, Angular checks every component for changes on every single browser event (click, keyup, etc.).OnPush tells Angular to only re-render the component when its @Input references change or an Observable emits.
Performance gain: In a list of 1,000 product cards, OnPush prevents 999 unnecessary checks when the user clicks a single button.
4. trackBy with *ngFor
Without trackBy, when the data array changes, Angular destroys and recreates ALL DOM elements. With trackBy, Angular only updates elements that actually changed.
5. Lazy Loading Modules
Already covered in Chapter 10, but worth emphasizing as the single biggest performance win for large apps:Result: Initial bundle might go from 5MB to 500KB, dramatically improving Time-to-Interactive.
6. Pure Pipes Over Methods in Templates
7. Async Pipe Instead of Manual Subscriptions
8. Virtual Scrolling for Large Lists
Only renders the items currently visible in the viewport:Rendering 10,000 items? Virtual scrolling renders only ~20 visible items at a time!
9. Bundle Optimization
10. Preloading Strategy
11. Common Mistakes
-
Method calls in templates: Methods in
{{ getXyz() }}are called on every change detection cycle. Use pipes orasyncinstead.
-
Not using
OnPushon presentational components: "Dumb" components that only receive@Inputshould always useOnPush.
12. MCQs with Answers
What is Angular's OnPush change detection strategy?
What is the performance benefit of trackBy in *ngFor?
What function does trackBy receive and what should it return?
What is the single biggest performance optimization for large Angular apps?
Why should you avoid method calls in templates like {{ getUsers() }}?
What is the Angular CDK feature for efficiently rendering large lists?
What Angular CLI command builds a production-optimized bundle?
What optimization technique does the production build use to remove unused code?
What preloading strategy loads lazy modules in the background after initial load?
Which components benefit most from OnPush change detection?
13. Interview Questions
-
Q: Explain
OnPushchange detection. What kind of components should use it and why?
- Q: What is virtual scrolling and why is it necessary for lists with thousands of items?
14. Summary
Performance optimization is not premature; it is professional engineering. By applyingOnPush, trackBy, lazy loading, the async pipe, and virtual scrolling strategically, Angular applications remain fast and responsive at enterprise scale. These optimizations are expected knowledge in any senior Angular interview.