Performance: Signals as modern state primitive
Signals impact how Angular tracks and updates UI state, affecting rendering speed and interaction responsiveness.
Jump into concepts and practice - no test required
import { Component, signal } from '@angular/core'; @Component({ selector: 'app-counter', template: `<button (click)="increment()">Increment</button><p>{{count()}}</p>` }) export class CounterComponent { count = signal(0); increment() { this.count.update(c => c + 1); } }
import { Component } from '@angular/core'; @Component({ selector: 'app-counter', template: `<button (click)="increment()">Increment</button><p>{{count}}</p>` }) export class CounterComponent { count = 0; increment() { this.count++; } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Traditional property state | Full component subtree checked | Multiple reflows if DOM changes | Higher paint cost due to broad updates | [X] Bad |
| Signal-based state | Only signal consumers updated | Minimal reflows limited to changed nodes | Lower paint cost due to targeted updates | [OK] Good |
signal() in Angular?signal()signal() creates a reactive value that Angular tracks for changes.signal() to UI updatessignal(initialValue) to create a signal with a starting value.const count = signal(10); matches the correct syntax. Others use incorrect constructors or methods.const count = signal(0); count.set(5); console.log(count());
count starts at 0, then set(5) changes its value to 5.count() returns the current value, which is 5 after the update.const name = signal('Alice');
name.update(name + ' Smith');update() expects a function that receives the current value and returns the new value.name + ' Smith' which is a string, not a function, causing an error.update() with a function that returns a new array including 7, which is correct.push() on the signal itself, which is not valid.