0
0
Vueframework~8 mins

onUpdated and onBeforeUpdate in Vue - Performance & Optimization

Choose your learning style9 modes available
Performance: onUpdated and onBeforeUpdate
MEDIUM IMPACT
These lifecycle hooks affect the rendering performance by running code before and after the DOM updates, impacting interaction responsiveness and visual stability.
Running expensive computations inside onUpdated or onBeforeUpdate hooks
Vue
import { onBeforeUpdate, onUpdated, nextTick } from 'vue';
onBeforeUpdate(() => {
  // minimal or no work here
});
onUpdated(async () => {
  await nextTick();
  // defer heavy work or use requestIdleCallback
});
Deferring heavy work until after the DOM is stable or offloading it prevents blocking the main thread during updates.
📈 Performance GainReduces blocking to under 10ms, improving interaction responsiveness and reducing layout shifts.
Running expensive computations inside onUpdated or onBeforeUpdate hooks
Vue
import { onBeforeUpdate, onUpdated } from 'vue';
onBeforeUpdate(() => {
  for (let i = 0; i < 1000000; i++) { /* heavy loop */ }
});
onUpdated(() => {
  // heavy DOM queries or recalculations
  document.querySelectorAll('.item').forEach(el => { /* complex logic */ });
});
Heavy synchronous work blocks the browser from painting updates quickly, causing slow interaction and possible layout shifts.
📉 Performance CostBlocks rendering for 50-100ms per update, causing janky UI and increased INP.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Heavy synchronous work in onBeforeUpdate/onUpdatedMultiple DOM queriesTriggers multiple reflowsHigh paint cost due to layout thrashing[X] Bad
Minimal work and deferred heavy tasksNo extra DOM queries during updateSingle reflow per update cycleLow paint cost, smooth rendering[OK] Good
Rendering Pipeline
onBeforeUpdate runs before the DOM updates, allowing preparation but blocking layout if heavy. onUpdated runs after DOM updates, where heavy work can delay painting or cause layout thrashing.
JavaScript Execution
Layout
Paint
⚠️ BottleneckJavaScript Execution blocking Layout and Paint
Core Web Vital Affected
INP, CLS
These lifecycle hooks affect the rendering performance by running code before and after the DOM updates, impacting interaction responsiveness and visual stability.
Optimization Tips
1Keep onBeforeUpdate and onUpdated hooks lightweight to avoid blocking rendering.
2Defer heavy computations using nextTick or idle callbacks after DOM updates.
3Avoid DOM queries or manipulations inside these hooks that cause layout thrashing.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance risk of running heavy synchronous code inside onBeforeUpdate?
AIt blocks the browser from painting updates quickly, causing slow UI response.
BIt increases the initial page load time significantly.
CIt causes network requests to delay.
DIt improves rendering speed by preparing data early.
DevTools: Performance
How to check: Record a performance profile while interacting with the component. Look for long tasks during the update phase and check if onBeforeUpdate or onUpdated hooks cause blocking.
What to look for: Long JavaScript execution bars during update, delayed frame rendering, and layout thrashing markers indicate poor performance.