Performance: Performance impact of change detection
Change detection affects how quickly Angular updates the UI after data changes, impacting interaction responsiveness and rendering speed.
import { Component, ChangeDetectionStrategy } from '@angular/core'; @Component({ selector: 'app-root', changeDetection: ChangeDetectionStrategy.OnPush, template: `<child-comp *ngFor="let item of items" [data]="item"></child-comp>` }) export class AppComponent { items = Array(1000).fill(0).map((_, i) => i); update() { this.items = this.items.map(x => x + 1); } }
import { Component } from '@angular/core'; @Component({ selector: 'app-root', template: `<child-comp *ngFor="let item of items" [data]="item"></child-comp>` }) export class AppComponent { items = Array(1000).fill(0).map((_, i) => i); update() { this.items = this.items.map(x => x + 1); } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Default change detection with many components | Many checks on all components | Multiple reflows if DOM updates | High paint cost due to frequent updates | [X] Bad |
| OnPush change detection with immutable inputs | Checks only changed components | Minimal reflows | Lower paint cost | [OK] Good |
@Component({
selector: 'app-counter',
template: `{{ counter }}`,
changeDetection: ChangeDetectionStrategy.OnPush
})
export class CounterComponent {
@Input() counter = 0;
}counter by setting the same number value again?Pass the test and this pattern moves unknown