Performance: Custom store logic
MEDIUM IMPACT
Custom store logic affects how reactive data updates propagate and how many DOM updates happen, impacting interaction responsiveness and rendering speed.
import { writable } from 'svelte/store'; function createGoodStore() { const { subscribe, set, update } = writable(0); return { subscribe, increment: () => { // defer heavy computation outside update setTimeout(() => { update(n => n + 1); }, 0); } }; } export const goodStore = createGoodStore();
import { writable } from 'svelte/store'; function createBadStore() { const { subscribe, set, update } = writable(0); return { subscribe, increment: () => { update(n => { // heavy computation inside update for (let i = 0; i < 1000000; i++) {} return n + 1; }); } }; } export const badStore = createBadStore();
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Heavy synchronous logic in store update | Multiple reactive DOM updates | Triggers multiple reflows | High paint cost due to blocking | [X] Bad |
| Lightweight async store update | Minimal reactive DOM updates | Single reflow per update | Low paint cost, smooth rendering | [OK] Good |