Performance: Computed signals for derived values
This affects how efficiently Angular updates the UI when derived data changes, impacting interaction responsiveness and rendering speed.
Jump into concepts and practice - no test required
const count = signal(0); const multiplier = signal(2); const derivedValue = computed(() => count() * multiplier()); // UI automatically updates when derivedValue changes effect(() => { updateUI(derivedValue()); });
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); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual separate effects for derived values | Multiple redundant DOM updates | Multiple reflows per dependency change | High paint cost due to repeated updates | [X] Bad |
| Computed signals for derived values | Single DOM update per change | Single reflow per change | Minimal paint cost with batched updates | [OK] Good |
computed signal in Angular's signal system?computed(() => ...) with a function returning the derived value.computed(() => price() + tax()). const total = signal(() => price + tax); wrongly uses signal and no function. const total = computed(price + tax); misses the function wrapper. const total = signal(price() + tax()); uses signal instead of computed.count.set(5) is called?const count = signal(0); const double = computed(() => count() * 2); console.log(double()); count.set(5); console.log(double());
count is 0, so double() returns 0 * 2 = 0.count.set(5)count to 5 updates double automatically. Calling double() now returns 5 * 2 = 10.const price = signal(100); const tax = signal(0.1); const total = computed(() => price + tax * price); console.log(total());
() to get their current value.price and tax directly without calling them, so it uses the signal objects, not their values.firstName and lastName. Which code correctly updates the full name when either signal changes and avoids unnecessary recomputations?firstName and lastName signals and update automatically when either changes.signal which won't update automatically. const fullName = computed(() => firstName + ' ' + lastName); uses signal variables directly without calling them. const fullName = computed(() => firstName() + lastName()); concatenates without space.