Bird
Raised Fist0
Angularframework~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of Angular's change detection?
easy
A. To manage routing between pages
B. To compile TypeScript code
C. To handle user authentication
D. To update the view when data changes

Solution

  1. Step 1: Understand change detection role

    Change detection in Angular watches for data changes to update the screen accordingly.
  2. Step 2: Identify the correct purpose

    Updating the view when data changes matches the role of change detection.
  3. Final Answer:

    To update the view when data changes -> Option D
  4. Quick Check:

    Change detection updates view [OK]
Hint: Change detection updates the screen on data change [OK]
Common Mistakes:
  • Confusing change detection with routing
  • Thinking it compiles code
  • Mixing it with authentication
2. Which syntax correctly sets the change detection strategy to OnPush in an Angular component?
easy
A. @Component({ detectionStrategy: OnPush })
B. @Component({ changeDetection: ChangeDetectionStrategy.Default })
C. @Component({ changeDetection: ChangeDetectionStrategy.OnPush })
D. @Component({ changeDetectionStrategy: 'OnPush' })

Solution

  1. Step 1: Recall correct property name

    The property to set change detection is 'changeDetection' inside @Component decorator.
  2. Step 2: Use correct enum value

    The enum value for OnPush is ChangeDetectionStrategy.OnPush, not a string or other property name.
  3. Final Answer:

    @Component({ changeDetection: ChangeDetectionStrategy.OnPush }) -> Option C
  4. Quick Check:

    Use changeDetection with ChangeDetectionStrategy.OnPush [OK]
Hint: Use changeDetection: ChangeDetectionStrategy.OnPush exactly [OK]
Common Mistakes:
  • Using wrong property name like detectionStrategy
  • Passing 'OnPush' as string
  • Confusing Default with OnPush
3. Given this Angular component with OnPush strategy:
  @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?
medium
A. The component view updates because input changed
B. The component view does not update because value is unchanged
C. Angular throws an error due to OnPush
D. The component view updates only after manual detectChanges call

Solution

  1. Step 1: Understand OnPush behavior with inputs

    OnPush triggers change detection only if input references change or events occur.
  2. Step 2: Same primitive value means no change

    Setting the same number value again does not change the input reference or value.
  3. Final Answer:

    The component view does not update because value is unchanged -> Option B
  4. Quick Check:

    OnPush skips unchanged input values [OK]
Hint: OnPush skips updates if input value unchanged [OK]
Common Mistakes:
  • Assuming view always updates on input set
  • Thinking OnPush causes errors
  • Believing manual detectChanges is always needed
4. You have an Angular component using Default change detection. The component updates slowly. Which fix improves performance?
medium
A. Switch to OnPush change detection strategy
B. Remove all @Input properties
C. Use setTimeout to delay updates
D. Disable change detection entirely

Solution

  1. Step 1: Identify performance issue cause

    Default strategy checks all components frequently, which can slow large apps.
  2. Step 2: Apply OnPush to reduce checks

    OnPush limits checks to when inputs change or events happen, improving speed.
  3. Final Answer:

    Switch to OnPush change detection strategy -> Option A
  4. Quick Check:

    OnPush improves performance by fewer checks [OK]
Hint: Use OnPush to reduce unnecessary checks [OK]
Common Mistakes:
  • Removing inputs breaks data flow
  • Using setTimeout doesn't fix detection speed
  • Disabling detection breaks UI updates
5. In a large Angular app, you want to optimize change detection. You have a parent component with Default strategy and a child with OnPush. The parent updates a complex object property but mutates it instead of replacing it. What is the impact on the child component's view update?
hard
A. Child view does not update because OnPush checks object reference
B. Child view updates because parent changed object property
C. Child view updates only if detectChanges is called manually
D. Child view updates automatically regardless of mutation

Solution

  1. Step 1: Understand OnPush and object mutation

    OnPush triggers on input reference changes, not internal mutations.
  2. Step 2: Mutation keeps same object reference

    Mutating object property does not change the reference, so OnPush skips update.
  3. Step 3: Child view remains unchanged without manual trigger

    Without new reference or manual detection, child view won't update.
  4. Final Answer:

    Child view does not update because OnPush checks object reference -> Option A
  5. Quick Check:

    OnPush needs new object reference to update [OK]
Hint: OnPush tracks object references, not internal mutations [OK]
Common Mistakes:
  • Assuming mutation triggers update
  • Thinking child updates automatically
  • Forgetting manual detectChanges option
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard