0
0
Angularframework~10 mins

Performance impact of change detection in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Performance impact of change detection
User Event or Async Data
Trigger Change Detection
Angular Checks Components
Evaluate Component Bindings
Update DOM if Needed
Render Updated View
Wait for Next Event
This flow shows how Angular runs change detection after events or data changes, checking components and updating the view, which affects performance.
Execution Sample
Angular
import { Component, signal } from '@angular/core';

@Component({
  selector: 'app-counter',
  standalone: true,
  template: `<button (click)="increment()">Count: {{count()}}</button>`
})
export class CounterComponent {
  count = signal(0);
  increment() { this.count.update(c => c + 1); }
}
A simple Angular component using signals where clicking the button increments a count, triggering change detection.
Execution Table
StepTriggerState BeforeActionState AfterWhat Re-rendersDOM Change
1Initial loadcount=0Render componentcount=0Button textButton shows 'Count: 0'
2User clicks buttoncount=0Call increment()count=1Button textButton updates to 'Count: 1'
3Change detection runscount=1Check bindingscount=1Button textDOM updated with new count
4User clicks button againcount=1Call increment()count=2Button textButton updates to 'Count: 2'
5Change detection runscount=2Check bindingscount=2Button textDOM updated with new count
6No further eventscount=2Idlecount=2No re-renderDOM unchanged
💡 No more user events, so change detection does not run again.
Variable Tracker
VariableStartAfter Step 2After Step 4Final
count0122
Key Moments - 2 Insights
Why does Angular check all components even if only one component's state changes?
Angular's default change detection strategy checks all components in the tree to ensure UI consistency, as shown in steps 3 and 5 where change detection runs after each increment.
How does using signals affect performance during change detection?
Signals allow Angular to track exactly which parts changed, so only affected components re-render, reducing unnecessary checks and DOM updates, as seen in the minimal re-rendering in steps 2 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of 'count' after step 4?
A1
B2
C0
D3
💡 Hint
Check the 'State After' column in row for step 4.
At which step does Angular update the DOM to show 'Count: 1'?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look for the step where 'DOM Change' shows update to 'Count: 1'.
If the component used OnPush change detection, how would the 'What Re-renders' column change?
AOnly components with changed inputs re-render
BNo components re-render
CAll components re-render every time
DDOM updates happen without re-render
💡 Hint
OnPush strategy limits re-rendering to components with changed inputs.
Concept Snapshot
Angular runs change detection after events or data changes.
It checks components to update the DOM if needed.
Default strategy checks all components, which can affect performance.
Using signals or OnPush strategy reduces unnecessary checks.
Efficient change detection improves app speed and responsiveness.
Full Transcript
This visual execution shows how Angular's change detection works and impacts performance. When a user clicks the button, the component's count signal updates. Angular then runs change detection, checking component bindings and updating the DOM if values changed. The table traces each step, showing state before and after, what re-renders, and DOM changes. Beginners often wonder why Angular checks all components; this is to keep UI consistent. Using signals helps Angular know exactly what changed, so it only updates necessary parts, improving performance. The quiz questions help reinforce understanding of state changes and rendering behavior.