0
0
Angularframework~8 mins

ngOnChanges for input changes in Angular - Performance & Optimization

Choose your learning style9 modes available
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.
Reacting to input property changes in a component
Angular
ngOnChanges(changes: SimpleChanges) {
  if (changes['importantInput'] && !changes['importantInput'].isFirstChange()) {
    // Minimal logic only when important input changes
    this.updateData();
  }
}
Checks for specific input changes and avoids running logic on initial or irrelevant changes, reducing unnecessary work.
📈 Performance Gainsingle reflow per relevant input change, improves INP by reducing blocking time
Reacting to input property changes in a component
Angular
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();
    }
  }
}
Running heavy logic or DOM updates on every input change triggers multiple reflows and slows interaction responsiveness.
📉 Performance Costtriggers multiple reflows per input change, blocks rendering for tens of milliseconds
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Heavy logic on all input changesMultiple DOM updatesMultiple reflows per changeHigh paint cost[X] Bad
Selective logic on specific inputsMinimal DOM updatesSingle reflow per relevant changeLow paint cost[OK] Good
Rendering Pipeline
When Angular detects input changes, ngOnChanges runs before the rendering pipeline updates the DOM. Heavy work here delays Style Calculation and Layout stages.
Change Detection
Style Calculation
Layout
Paint
⚠️ BottleneckLayout stage due to DOM updates triggered by ngOnChanges
Core Web Vital Affected
INP
This affects how quickly Angular detects and responds to changes in component inputs, impacting interaction responsiveness and rendering updates.
Optimization Tips
1Avoid heavy or synchronous DOM updates inside ngOnChanges.
2Check which inputs changed before running update logic.
3Keep ngOnChanges logic minimal to reduce layout thrashing.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance risk of running heavy logic inside ngOnChanges for every input change?
AIt increases bundle size significantly.
BIt delays initial page load only.
CIt causes multiple reflows and blocks rendering.
DIt improves interaction responsiveness.
DevTools: Performance
How to check: Record a performance profile while interacting with the component inputs. Look for long tasks during ngOnChanges execution.
What to look for: Long scripting times and multiple layout shifts indicate inefficient ngOnChanges handling.