Performance: ngOnChanges for input changes
MEDIUM IMPACT
This affects how quickly Angular detects and responds to changes in component inputs, impacting interaction responsiveness and rendering updates.
ngOnChanges(changes: SimpleChanges) {
if (changes['importantInput'] && !changes['importantInput'].isFirstChange()) {
// Minimal logic only when important input changes
this.updateData();
}
}ngOnChanges(changes: SimpleChanges) {
// Heavy logic or DOM manipulation on every input change
for (const propName in changes) {
if (changes.hasOwnProperty(propName)) {
// Expensive operation
this.updateDomOrData();
}
}
}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Heavy logic on all input changes | Multiple DOM updates | Multiple reflows per change | High paint cost | [X] Bad |
| Selective logic on specific inputs | Minimal DOM updates | Single reflow per relevant change | Low paint cost | [OK] Good |