Performance: Input signals and model signals
This concept affects how Angular updates the UI in response to user input and data changes, impacting interaction responsiveness and rendering speed.
Jump into concepts and practice - no test required
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); } }
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); } } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Multiple signal.set calls in loop | Multiple updates | Multiple reflows (equal to updates) | High paint cost | [X] Bad |
| Single batched signal.set call | Single update | Single reflow | Low paint cost | [OK] Good |
Input signals in Angular components?@Input() decorator followed by a signal initialization.@Input() decorator before the signal. const inputSignal = signal(); misses the decorator, C and D have invalid syntax.export class MyComponent {
@Input() count = signal(0);
increment() {
this.count.update(c => c + 1);
}
}count() after calling increment() twice if the initial value is 0?count starts at 0.increment() updates the signal by adding 1, so after two calls: 0 + 1 + 1 = 2.export class SampleComponent {
@Input() data = signal();
ngOnInit() {
this.data.set('Hello');
}
}data is declared without an initial value, which is invalid.signal() without arguments causes an error.userName and also maintains a local model signal greeting that updates automatically when userName changes. Which approach correctly implements this behavior?userName provides reactive data from parent. The local greeting should react automatically to changes.computed creates a signal that updates whenever userName() changes, keeping greeting in sync.