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.
import { onBeforeUpdate, onUpdated, nextTick } from 'vue'; onBeforeUpdate(() => { // minimal or no work here }); onUpdated(async () => { await nextTick(); // defer heavy work or use requestIdleCallback });
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 */ }); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Heavy synchronous work in onBeforeUpdate/onUpdated | Multiple DOM queries | Triggers multiple reflows | High paint cost due to layout thrashing | [X] Bad |
| Minimal work and deferred heavy tasks | No extra DOM queries during update | Single reflow per update cycle | Low paint cost, smooth rendering | [OK] Good |