Performance: Observable in component lifecycle
MEDIUM IMPACT
This affects interaction responsiveness and memory usage during component lifecycle events.
subscription = new Subscription(); ngOnInit() { this.subscription.add( this.dataService.getData().subscribe(data => { this.data = data; }) ); } ngOnDestroy() { this.subscription.unsubscribe(); }
ngOnInit() {
this.dataService.getData().subscribe(data => {
this.data = data;
});
}| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Subscribe without unsubscribe | No direct DOM ops but triggers repeated change detection | Multiple reflows if data updates frequently | High paint cost due to unnecessary updates | [X] Bad |
| Subscribe with unsubscribe on destroy | Minimal DOM ops triggered only on relevant data changes | Single or minimal reflows | Lower paint cost due to controlled updates | [OK] Good |