Performance: Signal creation and reading
MEDIUM IMPACT
This affects how quickly Angular updates the UI when data changes and how efficiently it tracks dependencies.
const count = signal(0); function increment() { count.set(count() + 1); } // Read signal once outside the loop const currentCount = count(); for(let i = 0; i < 1000; i++) { console.log(currentCount); }
const count = signal(0); function increment() { count.set(count() + 1); } // Reading signal inside a heavy loop or multiple times unnecessarily for(let i = 0; i < 1000; i++) { console.log(count()); }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Reading signal multiple times in loop | Low DOM ops | Triggers multiple change detection cycles | Low paint cost | [X] Bad |
| Reading signal once and caching value | Low DOM ops | Single change detection cycle | Low paint cost | [OK] Good |