Change Detection: Default vs. OnPush
CONCEPTS:Angular Change Detection
Question Variations
- "When would you choose `ChangeDetectionStrategy.OnPush` over the default strategy?"
- "What are the common pitfalls when using `OnPush` change detection?"
- "Explain the difference between `markForCheck()` and `detectChanges()` in `ChangeDetectorRef`."
- "How does using immutability in your state management help with Angular's performance?"
Why This Is Asked
Performance optimization is a key skill for senior Angular developers. Understanding how to reduce the number of change detection cycles using OnPush is essential for building scalable, high-performance applications. Interviewers want to see if you understand the triggers for change detection and how to manually manage it when necessary.
Key Concepts
- Directed Acyclic Graph (DAG): Angular’s component tree is checked from top to bottom.
- Immutability:
OnPushrelies on object reference changes. ChangeDetectorRef: Manually triggering detection viamarkForCheck()ordetectChanges().- Async Pipe: How it interacts with
OnPushto automatically trigger updates.
Question Variations
- “When would you choose
ChangeDetectionStrategy.OnPushover the default strategy?” - “What are the common pitfalls when using
OnPushchange detection?” - “Explain the difference between
markForCheck()anddetectChanges()inChangeDetectorRef.” - “How does using immutability in your state management help with Angular’s performance?”