Performance: Why the Composition API exists
MEDIUM IMPACT
This affects how Vue components load and update, impacting rendering speed and responsiveness.
import { ref, watch } from 'vue'; export default { setup() { const count = ref(0); function increment() { count.value++; } watch(count, (newVal) => { console.log('Count changed:', newVal); }); return { count, increment }; } }
export default { data() { return { count: 0 }; }, methods: { increment() { this.count++; } }, watch: { count(newVal) { console.log('Count changed:', newVal); } } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Options API scattered logic | More reactive dependencies | Multiple re-renders | Higher paint cost due to redundant updates | [X] Bad |
| Composition API grouped logic | Fewer reactive dependencies | Single targeted re-render | Lower paint cost with precise updates | [OK] Good |