Performance: Signal-based components
Signal-based components improve rendering speed by updating only the parts of the UI that depend on changed signals, reducing unnecessary work.
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 |
|---|---|---|---|---|
| Normal property with Angular change detection | Full component subtree checked | Multiple reflows if many bindings update | Higher paint cost due to more updates | [X] Bad |
| Signal-based reactive property | Only affected bindings update | Minimal reflows triggered | Lower paint cost due to targeted updates | [OK] Good |
signal() in Angular signal-based components?signal() doessignal() creates a reactive value that Angular tracks for changes.signal() to UI updatescount in Angular?set() method to assign a new value.countcount.set(5); to update the signal value.const count = signal(0); count.set(count() + 1); console.log(count());
count starts at 0.count.set(count() + 1); reads current value 0, adds 1, sets new value 1.console.log(count()); prints the updated value 1.const name = signal('Alice');
name.update('Bob');update() expects a function to modify the current value, not a direct value.name.update(value => 'Bob') or use set()."Learn Signals"?tasks holds an array, accessed by calling tasks().tasks.set().push() which returns length, not array; C assigns directly, breaking reactivity.