Performance: Effect for side effects
MEDIUM IMPACT
This affects how and when side effects run in Angular components, impacting interaction responsiveness and rendering stability.
import { Component, effect, signal } from '@angular/core'; @Component({ selector: 'app-example', template: `<p>{{count()}}</p>` }) export class ExampleComponent { count = signal(0); constructor() { effect(() => { const current = this.count(); document.body.style.backgroundColor = current % 2 === 0 ? 'lightblue' : 'lightgreen'; }); setInterval(() => this.count.update(c => c + 1), 1000); } }
import { Component, OnInit } from '@angular/core'; @Component({ selector: 'app-example', template: `<p>{{count}}</p>` }) export class ExampleComponent implements OnInit { count = 0; ngOnInit() { setInterval(() => { this.count++; // Direct DOM manipulation or heavy logic here document.body.style.backgroundColor = this.count % 2 === 0 ? 'lightblue' : 'lightgreen'; }, 1000); } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Direct DOM manipulation in ngOnInit | Multiple direct writes | Multiple reflows per interval | High paint cost due to style thrashing | [X] Bad |
| Angular effect with signals | Reactive updates only | Single reflow per update | Lower paint cost with batched updates | [OK] Good |