Performance: getContext
MEDIUM IMPACT
This affects how components share data without extra DOM nodes or prop drilling, impacting rendering speed and interaction responsiveness.
Parent.svelte: <script> import { setContext } from 'svelte'; setContext('theme', 'dark'); </script> <Child /> Child.svelte: <script> import { getContext } from 'svelte'; const theme = getContext('theme'); </script> <GrandChild /> GrandChild.svelte: <script> import { getContext } from 'svelte'; const theme = getContext('theme'); </script> <p>Theme is {theme}</p>
Parent.svelte: <script> let theme = 'dark'; </script> <Child theme={theme} /> Child.svelte: <script> export let theme; </script> <GrandChild theme={theme} /> GrandChild.svelte: <script> export let theme; </script> <p>Theme is {theme}</p>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Prop drilling through many components | Multiple prop updates across components | Multiple reflows triggered by each update | Higher paint cost due to repeated updates | [X] Bad |
| Using getContext for shared data | No extra DOM nodes or prop updates | Single reflow only when context consumer updates | Lower paint cost with fewer updates | [OK] Good |