0
0
Angularframework~8 mins

Input signals and model signals in Angular - Performance & Optimization

Choose your learning style9 modes available
Performance: Input signals and model signals
MEDIUM IMPACT
This concept affects how Angular updates the UI in response to user input and data changes, impacting interaction responsiveness and rendering speed.
Updating UI based on user input and model changes
Angular
import { Component, signal } from '@angular/core';

@Component({
  selector: 'app-counter',
  standalone: true,
  template: `
    <button (click)="increment()">Increment</button>
    <p>Count: {{ count() }}</p>
  `
})
export class CounterComponent {
  count = signal(0);

  increment() {
    // Batch updates by calculating new value first
    const newValue = this.count() + 5;
    this.count.set(newValue);
  }
}
Batching updates into a single signal set reduces the number of UI updates and re-renders.
📈 Performance GainTriggers 1 reflow and 1 paint per click, improving input responsiveness.
Updating UI based on user input and model changes
Angular
import { Component, signal } from '@angular/core';

@Component({
  selector: 'app-counter',
  standalone: true,
  template: `
    <button (click)="increment()">Increment</button>
    <p>Count: {{ count() }}</p>
  `
})
export class CounterComponent {
  count = signal(0);

  increment() {
    // Directly mutating the signal multiple times in a loop
    for (let i = 0; i < 5; i++) {
      this.count.set(this.count() + 1);
    }
  }
}
Multiple direct updates to the signal cause Angular to schedule many UI updates, triggering multiple re-renders and slowing interaction.
📉 Performance CostTriggers 5 reflows and 5 paints per click, increasing input latency.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Multiple signal.set calls in loopMultiple updatesMultiple reflows (equal to updates)High paint cost[X] Bad
Single batched signal.set callSingle updateSingle reflowLow paint cost[OK] Good
Rendering Pipeline
Input signals trigger Angular's reactive system to update the model, which then schedules UI updates. The browser recalculates styles, layouts, and paints the updated UI.
Style Calculation
Layout
Paint
Composite
⚠️ BottleneckLayout and Paint stages due to multiple signal updates causing repeated reflows.
Core Web Vital Affected
INP
This concept affects how Angular updates the UI in response to user input and data changes, impacting interaction responsiveness and rendering speed.
Optimization Tips
1Batch multiple signal updates into one to reduce reflows.
2Avoid direct multiple signal.set calls inside loops.
3Use Angular's reactive system efficiently to improve input responsiveness.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance issue when updating an Angular signal multiple times in a loop?
AIt increases the bundle size significantly.
BIt causes multiple reflows and paints, slowing down input responsiveness.
CIt causes the browser to skip rendering updates.
DIt improves the Largest Contentful Paint (LCP) metric.
DevTools: Performance
How to check: Record a performance profile while interacting with the component. Look for multiple Layout and Paint events triggered by repeated signal updates.
What to look for: Multiple consecutive reflows and paints indicate inefficient signal updates; a single reflow and paint per interaction shows good performance.