Performance: Setup function basics
MEDIUM IMPACT
This affects the initial rendering speed and reactivity setup of Vue components.
import { reactive, computed } from 'vue'; export default { setup() { const state = reactive({ count: 0 }); const doubled = computed(() => state.count * 2); return { state, doubled }; } }
import { reactive, computed } from 'vue'; export default { setup() { const state = reactive({ count: 0 }); const doubled = computed(() => { // heavy computation inside computed let result = 0; for (let i = 0; i < 1000000; i++) { result += state.count * 2; } return result; }); return { state, doubled }; } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Heavy computation in setup | Minimal | 0 | High due to JS blocking | [X] Bad |
| Simple reactive state in setup | Minimal | 0 | Low | [OK] Good |
| Data fetching inside setup | Minimal | 0 | Blocks LCP until data arrives | [X] Bad |
| Data fetching in onMounted hook | Minimal | 0 | Non-blocking initial paint | [OK] Good |