Discover how Angular keeps your app lightning fast by smartly updating only what really needs it!
Why Performance impact of change detection in Angular? - Purpose & Use Cases
Imagine you have a big webpage with many parts that update when data changes. You try to check and update each part manually every time something changes.
Manually checking and updating every part is slow and confusing. It can cause the page to freeze or lag, and you might miss some updates or update too many times.
Angular's change detection automatically tracks what needs updating and only refreshes those parts, making the page fast and smooth without extra work.
if(dataChanged) { updateAllUI(); }Angular runs change detection and updates only changed parts automatically.This lets your app stay fast and responsive even as it grows bigger and more complex.
Think of a social media feed where new posts appear without slowing down your scrolling or freezing the page.
Manual UI updates are slow and error-prone.
Angular's change detection updates only what changed.
This improves app speed and user experience.
Practice
Solution
Step 1: Understand change detection role
Change detection in Angular watches for data changes to update the screen accordingly.Step 2: Identify the correct purpose
Updating the view when data changes matches the role of change detection.Final Answer:
To update the view when data changes -> Option DQuick Check:
Change detection updates view [OK]
- Confusing change detection with routing
- Thinking it compiles code
- Mixing it with authentication
Solution
Step 1: Recall correct property name
The property to set change detection is 'changeDetection' inside @Component decorator.Step 2: Use correct enum value
The enum value for OnPush is ChangeDetectionStrategy.OnPush, not a string or other property name.Final Answer:
@Component({ changeDetection: ChangeDetectionStrategy.OnPush }) -> Option CQuick Check:
Use changeDetection with ChangeDetectionStrategy.OnPush [OK]
- Using wrong property name like detectionStrategy
- Passing 'OnPush' as string
- Confusing Default with OnPush
@Component({
selector: 'app-counter',
template: `{{ counter }}`,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class CounterComponent {
@Input() counter = 0;
}What happens if the parent updates
counter by setting the same number value again?Solution
Step 1: Understand OnPush behavior with inputs
OnPush triggers change detection only if input references change or events occur.Step 2: Same primitive value means no change
Setting the same number value again does not change the input reference or value.Final Answer:
The component view does not update because value is unchanged -> Option BQuick Check:
OnPush skips unchanged input values [OK]
- Assuming view always updates on input set
- Thinking OnPush causes errors
- Believing manual detectChanges is always needed
Solution
Step 1: Identify performance issue cause
Default strategy checks all components frequently, which can slow large apps.Step 2: Apply OnPush to reduce checks
OnPush limits checks to when inputs change or events happen, improving speed.Final Answer:
Switch to OnPush change detection strategy -> Option AQuick Check:
OnPush improves performance by fewer checks [OK]
- Removing inputs breaks data flow
- Using setTimeout doesn't fix detection speed
- Disabling detection breaks UI updates
Solution
Step 1: Understand OnPush and object mutation
OnPush triggers on input reference changes, not internal mutations.Step 2: Mutation keeps same object reference
Mutating object property does not change the reference, so OnPush skips update.Step 3: Child view remains unchanged without manual trigger
Without new reference or manual detection, child view won't update.Final Answer:
Child view does not update because OnPush checks object reference -> Option AQuick Check:
OnPush needs new object reference to update [OK]
- Assuming mutation triggers update
- Thinking child updates automatically
- Forgetting manual detectChanges option
Ready to prove it?
Pass the test and this pattern moves unknown
