Performance: Pinia vs Vuex comparison
This affects the app's state management performance, impacting interaction responsiveness and bundle size.
import { defineStore } from 'pinia'; const useCounterStore = defineStore('counter', { state: () => ({ count: 0 }), actions: { increment() { this.count++ }, incrementAsync() { setTimeout(() => this.increment(), 1000) } } });
import { createStore } from 'vuex'; const store = createStore({ state: () => ({ count: 0 }), mutations: { increment(state) { state.count++ } }, actions: { incrementAsync({ commit }) { setTimeout(() => commit('increment'), 1000) } } });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Vuex | More reactive watchers due to mutations and actions | Multiple reflows possible on complex state changes | Higher paint cost from frequent updates | [!] OK |
| Pinia | Fewer reactive watchers with direct state mutation | Single reflow per state change | Lower paint cost due to optimized updates | [OK] Good |
console.log(store.count) after calling store.increment() once?const useStore = defineStore('main', {
state: () => ({ count: 0 }),
actions: {
increment() {
this.count++
}
}
})
const store = useStore()
store.increment()const store = new Vuex.Store({
state: { count: 0 },
mutations: {
increment(state) {
state.count++
}
}
})
store.increment()store.increment() with store.commit('increment') to correctly invoke the mutation.increment(state) { state.count++ } into Pinia's style?this.this.count directly.this.count++. -> Option BPass the test and this pattern moves unknown