Performance: When to use OnPush
HIGH IMPACT
OnPush affects how Angular detects changes and updates the DOM, impacting rendering speed and responsiveness.
import { Component, ChangeDetectionStrategy, Input } from '@angular/core'; @Component({ selector: 'app-good', changeDetection: ChangeDetectionStrategy.OnPush, template: `<p>{{data.name}}</p>` }) export class GoodComponent { @Input() data!: { name: string }; }
import { Component } from '@angular/core'; @Component({ selector: 'app-bad', template: `<p>{{data.name}}</p>` }) export class BadComponent { data = { name: 'Angular' }; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Default Change Detection | Runs on all events and async tasks | Multiple reflows per event | Higher paint cost due to frequent updates | [X] Bad |
| OnPush Change Detection | Runs only on input reference changes or events | Minimal reflows | Lower paint cost with fewer updates | [OK] Good |