Performance: onBeforeMount and onBeforeUnmount
MEDIUM IMPACT
These lifecycle hooks affect the timing of component setup and teardown, impacting initial render speed and cleanup efficiency.
import { onBeforeMount, onBeforeUnmount } from 'vue'; let timerId; onBeforeMount(() => { // defer heavy work asynchronously timerId = setTimeout(() => { // heavy data processing }, 0); }); onBeforeUnmount(() => { // cleanup done asynchronously or minimal clearTimeout(timerId); });
import { onBeforeMount, onBeforeUnmount } from 'vue'; onBeforeMount(() => { // heavy synchronous data processing for(let i = 0; i < 1e7; i++) {} }); onBeforeUnmount(() => { // heavy cleanup blocking UI while(true) {} });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Heavy synchronous code in onBeforeMount/onBeforeUnmount | Minimal | 0 but blocks JS thread | Blocks paint and input | [X] Bad |
| Light or async code in onBeforeMount/onBeforeUnmount | Minimal | 0, non-blocking | Smooth paint and input | [OK] Good |