Performance: Options API vs Composition API decision
This affects initial page load speed and runtime rendering performance by influencing bundle size and reactivity system efficiency.
import { ref, onMounted } from 'vue'; export default { setup() { const count = ref(0); function increment() { count.value++; } onMounted(() => { console.log('Component mounted'); }); return { count, increment }; } }
export default { data() { return { count: 0 }; }, methods: { increment() { this.count++; } }, mounted() { console.log('Component mounted'); } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Options API | Standard reactive bindings | Moderate reflows on state change | Moderate paint cost | [!] OK |
| Composition API | More granular reactive bindings | Fewer reflows due to optimized dependencies | Lower paint cost | [OK] Good |
data, methods, and computed?data, methods, and computed.setup() function by logical features, not by options.ref() inside setup(), e.g., const count = ref(0);.import { ref } from 'vue';
export default {
setup() {
const count = ref(0);
function increment() {
count.value++;
}
return { count, increment };
}
}count after calling increment() twice?count is a ref initialized to 0. Calling increment() increases count.value by 1 each time.export default {
data() {
count: 0
},
methods: {
increment() {
this.count++;
}
}
}data() must return an object. Here, it lacks return keyword.this.count are correct. data() is correctly a function.setup(), making reuse easier. Options API separates by options, which can scatter related logic.Pass the test and this pattern moves unknown