Performance: Why stores manage shared state
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 |
import { writable } from 'svelte/store';
const count = writable(0);
count.subscribe(value => console.log(value));
count.set(5);
count.update(n => n + 1);import { writable } from 'svelte/store';
const user = writable({ name: 'Alice' });
// In component
user.name = 'Bob';Pass the test and this pattern moves unknown