Performance: onMounted and onUnmounted
MEDIUM IMPACT
These lifecycle hooks affect the timing of DOM interactions and resource cleanup, impacting rendering responsiveness and memory usage.
import { onMounted, onUnmounted } from 'vue'; onMounted(() => { window.addEventListener('resize', onResize); }); onUnmounted(() => { window.removeEventListener('resize', onResize); });
import { onMounted } from 'vue'; onMounted(() => { window.addEventListener('resize', onResize); }); // No cleanup on unmount
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Add event listener in onMounted only | 1 addEventListener | 0 reflows | 0 paint | [X] Bad |
| Add in onMounted and remove in onUnmounted | 1 addEventListener + 1 removeEventListener | 0 reflows | 0 paint | [OK] Good |