Performance: Why stores manage shared state
MEDIUM IMPACT
This affects how efficiently the app updates UI when multiple components share data, impacting interaction responsiveness and rendering speed.
import { writable } from 'svelte/store'; export const count = writable(0); // Components subscribe to 'count' store and update reactively function increment() { count.update(n => n + 1); }
let count = 0; // Each component imports and modifies 'count' directly without a store function increment() { count += 1; // No reactive updates across components }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Direct shared variable modification | Multiple manual DOM updates | Multiple reflows per update | High paint cost due to redundant updates | [X] Bad |
| Using Svelte writable stores | Reactive updates only on subscribed components | Single reflow per state change | Low paint cost with efficient batching | [OK] Good |