Performance: Default change detection strategy
This affects how often Angular checks and updates the UI, impacting interaction responsiveness and CPU usage.
import { Component, ChangeDetectionStrategy } from '@angular/core'; @Component({ selector: 'app-root', template: `<child-comp></child-comp>`, changeDetection: ChangeDetectionStrategy.OnPush }) export class AppComponent {} // OnPush strategy limits checks to input changes and events inside component
import { Component, ChangeDetectionStrategy } from '@angular/core'; @Component({ selector: 'app-root', template: `<child-comp></child-comp>`, changeDetection: ChangeDetectionStrategy.Default }) export class AppComponent {} // Default strategy triggers change detection for all components on every event
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Default change detection | Checks all components every event | Triggers multiple reflows if many updates | High paint cost if many changes | [X] Bad |
| OnPush change detection | Checks only changed components | Minimal reflows | Lower paint cost | [OK] Good |
@Component({
selector: 'app-counter',
template: `Count: {{count}}`
})
export class CounterComponent {
count = 0;
increment() {
this.count++;
}
}@Component({
selector: 'app-sample',
template: `{{value}}`
})
export class SampleComponent {
value = 10;
updateValue() {
this.value = 20;
}
}updateValue(), the view does not update. What is the most likely cause?Pass the test and this pattern moves unknown