0
0
Angularframework~8 mins

Computed signals for derived values in Angular - Performance & Optimization

Choose your learning style9 modes available
Performance: Computed signals for derived values
MEDIUM IMPACT
This affects how efficiently Angular updates the UI when derived data changes, impacting interaction responsiveness and rendering speed.
Updating UI based on derived state from multiple signals
Angular
const count = signal(0);
const multiplier = signal(2);
const derivedValue = computed(() => count() * multiplier());

// UI automatically updates when derivedValue changes
effect(() => {
  updateUI(derivedValue());
});
Computed signal caches and recalculates only when dependencies change, batching updates efficiently.
📈 Performance GainSingle recalculation and UI update per change, reducing INP latency significantly.
Updating UI based on derived state from multiple signals
Angular
const count = signal(0);
const multiplier = signal(2);

// Manually using separate effects to update derived value
let derivedValue = 0;
effect(() => {
  derivedValue = count() * multiplier();
  updateUI(derivedValue);
});
Manually using separate effects causes multiple recalculations and UI updates, leading to redundant work and slower interaction.
📉 Performance CostTriggers multiple recalculations and DOM updates per change, increasing INP latency.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Manual separate effects for derived valuesMultiple redundant DOM updatesMultiple reflows per dependency changeHigh paint cost due to repeated updates[X] Bad
Computed signals for derived valuesSingle DOM update per changeSingle reflow per changeMinimal paint cost with batched updates[OK] Good
Rendering Pipeline
Computed signals optimize the update flow by recalculating derived values only when their dependencies change, minimizing layout and paint work.
Style Calculation
Layout
Paint
⚠️ BottleneckLayout and Paint stages due to unnecessary recalculations and DOM updates
Core Web Vital Affected
INP
This affects how efficiently Angular updates the UI when derived data changes, impacting interaction responsiveness and rendering speed.
Optimization Tips
1Use computed signals to memoize derived values and avoid redundant recalculations.
2Avoid manual subscriptions that cause multiple updates for the same derived data.
3Batch UI updates by relying on computed signals to improve interaction responsiveness.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of using computed signals for derived values in Angular?
AThey recalculate derived values only when dependencies change, reducing unnecessary work.
BThey increase the number of DOM updates to keep UI fresh.
CThey block rendering until all signals update.
DThey add extra subscriptions for better debugging.
DevTools: Performance
How to check: Record a performance profile while interacting with the UI that updates derived values. Look for multiple recalculations or layout thrashing in the flame chart.
What to look for: Fewer recalculations and shorter layout/paint times indicate good use of computed signals.