Performance: Async pipe for template subscriptions
MEDIUM IMPACT
This affects how Angular handles observable subscriptions in templates, impacting rendering speed and memory usage.
export class MyComponent { data$ = this.service.getData(); constructor(private service: DataService) {} } <!-- Template --> <div>{{ data$ | async }}</div>
import { Subscription } from 'rxjs'; export class MyComponent { data: any; subscription: Subscription; constructor(private service: DataService) {} ngOnInit() { this.subscription = this.service.getData().subscribe(value => { this.data = value; }); } ngOnDestroy() { this.subscription.unsubscribe(); } } <!-- Template --> <div>{{ data }}</div>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual subscription with property binding | Multiple DOM updates as data changes | Triggers reflow on each update | Moderate paint cost due to frequent updates | [X] Bad |
| Async pipe in template | Single DOM update per data emission | Minimal reflows triggered | Lower paint cost due to optimized updates | [OK] Good |