Performance: ngOnDestroy for cleanup
MEDIUM IMPACT
This affects the interaction responsiveness and memory usage by properly cleaning up resources when components are destroyed.
import { Component, OnDestroy } from '@angular/core'; import { Subscription } from 'rxjs'; @Component({ selector: 'app-example', template: '' }) export class ExampleComponent implements OnDestroy { subscription: Subscription = someObservable.subscribe(); timerId = setInterval(() => console.log('tick'), 1000); ngOnDestroy() { this.subscription.unsubscribe(); clearInterval(this.timerId); } }
import { Component } from '@angular/core'; @Component({ selector: 'app-example', template: '' }) export class ExampleComponent { subscription = someObservable.subscribe(); timerId = setInterval(() => console.log('tick'), 1000); // No ngOnDestroy implemented }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No cleanup in ngOnDestroy | No direct DOM ops but leaves event listeners active | Triggers extra reflows indirectly due to background tasks | Increases paint cost due to unnecessary JS work | [X] Bad |
| Proper cleanup in ngOnDestroy | Removes event listeners and timers | No extra reflows triggered | Minimal paint cost, efficient rendering | [OK] Good |