How does Angular change detection work, and what is the OnPush strategy?
Angular's change detection is the mechanism that keeps the DOM in sync with the application state. After every browser event (click, keyup, HTTP response, timer), Angular runs change detection — walking the component tree from root to leaves and updating any bindings whose values changed.
Angular checks every component in the tree after every event — safe but expensive for large apps.
@Component({ changeDetection: ChangeDetectionStrategy.Default })
// Checked on every event, regardless of what changed
Angular only checks the component when:
@Input() reference changesasync pipe emits a new valueChangeDetectorRef.markForCheck() is explicitly called@Component({
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ProductCardComponent {
@Input() product!: Product;
// Only re-checked when product reference changes
}
Angular uses Zone.js to monkey-patch browser APIs (setTimeout, fetch, event listeners) so it knows when to run change detection. Angular 17+ introduced experimental Zoneless mode using Signals, which removes Zone.js entirely for better performance.
// With signals, Angular precisely knows which component to update
readonly count = signal(0);
// Only components that read count() are re-rendered when it changes
All Comments