Performance: Why lifecycle hooks matter
MEDIUM IMPACT
Lifecycle hooks affect how and when Angular components update, impacting rendering speed and responsiveness.
ngOnChanges(changes) {
if (changes['inputData']) {
this.calculateSomethingExpensive();
}
}ngDoCheck() {
// heavy computation or DOM manipulation here
this.calculateSomethingExpensive();
}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Heavy logic in ngDoCheck | Many unnecessary DOM reads/writes | Multiple reflows per cycle | High paint cost due to layout thrashing | [X] Bad |
| Conditional logic in ngOnChanges | Minimal DOM operations only on input change | Single reflow when needed | Low paint cost | [OK] Good |