Performance: Lifecycle hooks in Composition API
MEDIUM IMPACT
This affects the timing of code execution during component rendering and updates, impacting interaction responsiveness and rendering smoothness.
import { onMounted } from 'vue'; onMounted(() => { setTimeout(() => { // heavy task deferred for (let i = 0; i < 1_000_000_000; i++) {} }, 0); });
import { onMounted } from 'vue'; onMounted(() => { // heavy synchronous task for (let i = 0; i < 1_000_000_000; i++) {} });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Heavy synchronous code in onMounted | Minimal | Blocks rendering causing multiple reflows | High due to delayed paint | [X] Bad |
| Deferred heavy code with setTimeout | Minimal | Single reflow after render | Low paint cost | [OK] Good |
| Direct DOM style changes in multiple hooks | Multiple style changes | 2+ reflows per update | High paint cost causing jank | [X] Bad |
| Reactive state with watchEffect for DOM updates | Single batched DOM update | Single reflow | Low paint cost | [OK] Good |