Change detection helps Angular know when to update the screen. But if it runs too often or checks too much, it can slow down your app.
Performance impact of change detection in Angular
import { ChangeDetectionStrategy, Component } from '@angular/core'; @Component({ selector: 'app-example', templateUrl: './example.component.html', changeDetection: ChangeDetectionStrategy.OnPush }) export class ExampleComponent {}
Use ChangeDetectionStrategy.OnPush to tell Angular to check only when inputs change.
The default is ChangeDetectionStrategy.Default, which checks often but can be slower.
import { Component, ChangeDetectionStrategy } from '@angular/core'; @Component({ selector: 'app-default', template: `<p>{{ counter }}</p>`, changeDetection: ChangeDetectionStrategy.Default }) export class DefaultComponent { counter = 0; }
counter input changes.import { Component, ChangeDetectionStrategy, Input } from '@angular/core'; @Component({ selector: 'app-onpush', template: `<p>{{ counter }}</p>`, changeDetection: ChangeDetectionStrategy.OnPush }) export class OnPushComponent { @Input() counter = 0; }
This example shows a button that increases a number. The CounterComponent uses OnPush change detection, so it updates only when the input count changes.
This helps Angular skip unnecessary checks and improves performance.
import { Component, ChangeDetectionStrategy, Input } from '@angular/core'; @Component({ selector: 'app-counter', template: `<p>Count: {{ count }}</p>`, standalone: true, changeDetection: ChangeDetectionStrategy.OnPush }) export class CounterComponent { @Input() count = 0; } @Component({ selector: 'app-root', template: ` <button (click)="increment()">Increment</button> <app-counter [count]="counter"></app-counter> `, standalone: true, imports: [CounterComponent] }) export class AppComponent { counter = 0; increment() { this.counter++; } }
OnPush works best when inputs are immutable or changed by replacing the object.
Too many checks slow down the app, so use OnPush to reduce work.
Use Angular DevTools to see how often change detection runs.
Change detection updates the screen when data changes.
Default strategy checks often but can slow down big apps.
OnPush strategy improves speed by checking less often.
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
