Performance: Persisting store state
MEDIUM IMPACT
This concept affects page load speed and interaction responsiveness by managing how store data is saved and restored across sessions.
import { createStore } from 'vuex'; const store = createStore({ state() { return { count: 0 }; }, mutations: { increment(state) { state.count++; } }, plugins: [store => { const savedCount = localStorage.getItem('count'); if (savedCount) { store.replaceState({ count: JSON.parse(savedCount) }); } window.addEventListener('beforeunload', () => { localStorage.setItem('count', JSON.stringify(store.state.count)); }); }] });
import { createStore } from 'vuex'; const store = createStore({ state() { return { count: 0 }; }, mutations: { increment(state) { state.count++; localStorage.setItem('count', JSON.stringify(state.count)); } }, plugins: [store => { const savedCount = localStorage.getItem('count'); if (savedCount) { store.replaceState({ count: JSON.parse(savedCount) }); } }] });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Saving state on every mutation synchronously | Minimal | Multiple reflows on load | High due to blocking | [X] Bad |
| Load once on startup, save on unload | Minimal | Single reflow | Low blocking | [OK] Good |