Performance: OnPush change detection strategy
HIGH IMPACT
This affects how often Angular checks and updates the component's view, impacting rendering speed and responsiveness.
import { Component, ChangeDetectionStrategy, Input } from '@angular/core'; @Component({ selector: 'app-example', template: `<p>{{ counter }}</p>`, changeDetection: ChangeDetectionStrategy.OnPush }) export class ExampleComponent { @Input() counter = 0; }
import { Component } from '@angular/core'; @Component({ selector: 'app-example', template: `<p>{{ counter }}</p>` }) export class ExampleComponent { counter = 0; // Default change detection strategy (CheckAlways) }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Default (CheckAlways) | Many unnecessary DOM checks | Multiple reflows on unrelated changes | Higher paint cost due to frequent updates | [X] Bad |
| OnPush Change Detection | Checks only on input or event changes | Minimal reflows limited to actual changes | Lower paint cost with fewer updates | [OK] Good |