0
0
Vueframework~8 mins

Watch vs computed decision in Vue - Performance Comparison

Choose your learning style9 modes available
Performance: Watch vs computed decision
MEDIUM IMPACT
This affects how efficiently Vue updates the DOM and manages reactive data changes, impacting interaction responsiveness and rendering speed.
Deriving a value from reactive data to display in the template
Vue
const derived = computed(() => props.value * 2);
Computed caches the result and only recalculates when dependencies change, reducing unnecessary updates.
📈 Performance GainSingle reactive update with cached value, fewer DOM patches
Deriving a value from reactive data to display in the template
Vue
watch(() => props.value, (newVal) => {
  derived.value = newVal * 2;
});
Triggers side effects and updates manually, causing extra reactivity overhead and potential multiple DOM updates.
📉 Performance CostTriggers multiple reactivity updates and possible extra DOM patches
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Using computed for derived dataMinimal reactive updatesSingle reflow per changeLow paint cost[OK] Good
Using watch to update derived data manuallyMultiple reactive updatesMultiple reflows possibleHigher paint cost[X] Bad
Expensive operations inside computedBlocks reactive updatesBlocks reflowHigh paint cost[X] Bad
Expensive operations inside watchRuns after reactive updateMay cause extra reflowsModerate paint cost[!] OK
Rendering Pipeline
Reactive data changes trigger dependency tracking. Computed properties recalculate during the 'Reactive Update' phase, causing minimal layout recalculations. Watchers run after updates and can trigger additional side effects, potentially causing extra layout and paint cycles.
Reactive Update
Layout
Paint
⚠️ BottleneckExtra side effects in watchers can cause multiple layout recalculations and paints.
Core Web Vital Affected
INP
This affects how efficiently Vue updates the DOM and manages reactive data changes, impacting interaction responsiveness and rendering speed.
Optimization Tips
1Use computed properties for pure derived reactive data to leverage caching.
2Use watchers only for side effects or expensive operations triggered by reactive changes.
3Avoid expensive computations inside computed properties to keep UI responsive.
Performance Quiz - 3 Questions
Test your performance knowledge
Which pattern is best for deriving reactive data that updates the UI?
AUse methods called in the template
BUse watchers to update derived data manually
CUse computed properties for derived data
DUse setTimeout to update data
DevTools: Performance
How to check: Record a performance profile while interacting with the component. Look for long tasks or multiple reactive updates triggered by computed or watch.
What to look for: Check for multiple recalculations or layout thrashing caused by watchers updating reactive data unnecessarily.