Performance: Composable vs mixin comparison
This affects page load speed and interaction responsiveness by influencing JavaScript execution and component rendering.
import { ref, onMounted } from 'vue'; export function useCounter() { const count = ref(0); function increment() { count.value++; } onMounted(() => { console.log('Composable mounted'); }); return { count, increment }; } export default { setup() { const { count, increment } = useCounter(); return { count, increment }; } }
const myMixin = { created() { console.log('Mixin created'); }, methods: { sharedMethod() { console.log('Shared logic'); } } } export default { mixins: [myMixin], data() { return { count: 0 }; }, methods: { increment() { this.count++; } } }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Mixin | No direct DOM ops, but causes duplicated reactive setups | Multiple reactivity recalculations | Minimal paint impact | [X] Bad |
| Composable | No direct DOM ops, single reactive setup per usage | Single reactivity recalculation | Minimal paint impact | [OK] Good |
mixin and a composable in Vue?const myMixin = {
data() { return { count: 0 }; },
methods: { increment() { this.count++; console.log(this.count); } }
}
export default {
mixins: [myMixin],
template: `Click`
}import { ref } from 'vue';
function useTimer() {
const time = ref(0);
setInterval(() => time.value++, 1000);
return { time };
}
export default {
setup() {
const timer = useTimer;
return { timer };
},
template: `{{ timer.time }}`
}Pass the test and this pattern moves unknown