Performance: Subscribing to observables
MEDIUM IMPACT
This affects interaction responsiveness and memory usage by controlling how data streams update the UI and how resources are managed.
private subscription = this.dataService.getData().subscribe(data => { this.items = data; }); ngOnDestroy() { this.subscription.unsubscribe(); }
this.dataService.getData().subscribe(data => { this.items = data; });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual subscribe without unsubscribe | Multiple updates even after component destroyed | Multiple reflows due to repeated change detection | High paint cost from unnecessary UI updates | [X] Bad |
| Manual subscribe with unsubscribe in ngOnDestroy | Updates only while component alive | Single reflow per update cycle | Moderate paint cost | [!] OK |
| Using async pipe in template | Updates only when data changes and auto cleans up | Minimal reflows | Low paint cost | [OK] Good |