Performance: Observable vs Promise mental model
MEDIUM IMPACT
This concept affects how asynchronous data is handled, impacting interaction responsiveness and rendering updates in Angular apps.
fetchData(): Observable<Data> {
return this.http.get<Data>(url);
}
ngOnInit() {
this.fetchData().subscribe(data => {
this.data = data;
// UI updates reactively on each emission
});
}fetchData(): Promise<Data> { return this.http.get<Data>(url).toPromise(); } ngOnInit() { this.fetchData().then(data => { this.data = data; // No further updates possible }); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Promise (single resolution) | Single DOM update | 1 reflow | 1 paint | [OK] Good for single async data |
| Observable (multiple emissions) | Multiple DOM updates | Multiple reflows | Multiple paints | [!] OK if managed |
| Observable without throttling | Many DOM updates | Many reflows | High paint cost | [X] Bad if unthrottled |