Performance: Context with stores
MEDIUM IMPACT
This affects how efficiently state is shared and updated across components without unnecessary DOM updates or re-renders.
import { writable } from 'svelte/store'; import { setContext, getContext } from 'svelte'; const count = writable(0); setContext('countStore', count); // In nested components const count = getContext('countStore');
import { writable } from 'svelte/store'; const count = writable(0); // Passing store as a prop through many components <ComponentA count={count} /> // Each intermediate component passes count down as prop
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Passing stores via props through many components | Many prop updates and component re-renders | Multiple reflows triggered by each update | Higher paint cost due to repeated updates | [X] Bad |
| Providing stores via Svelte context | Only subscribed components update | Single reflow per store update | Lower paint cost due to targeted updates | [OK] Good |