Performance: Migrating from observables to signals
This affects interaction responsiveness and rendering efficiency by changing how Angular tracks and updates reactive data.
Jump into concepts and practice - no test required
data = signal(this.service.getInitialData()); this.service.dataStream.subscribe(value => data.set(value));
this.data$ = this.service.getData(); this.data$.subscribe(value => { this.data = value; this.cd.detectChanges(); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Observable with manual subscription | Multiple updates per data change | Multiple reflows if DOM updates frequently | Higher paint cost due to redundant updates | [X] Bad |
| Signal-based reactive data | Minimal updates only on value change | Single reflow per update | Lower paint cost due to precise updates | [OK] Good |
Observable to signal() in Angular?signal() function to create reactive signals.const count = signal(0); matches Angular's official syntax.const count = signal(0);
function increment() {
count.set(count() + 1);
}
increment();
increment();
console.log(count());count starts at 0. Each call to increment() adds 1.const count = signal(0); count.subscribe(value => console.log(value)); count.set(5);
subscribe method; that is an observable feature.count$ = new BehaviorSubject(0);
increment() {
this.count$.next(this.count$.value + 1);
}signal(0) to create a reactive value starting at 0.count.set(count() + 1) to update the signal's value correctly.