Performance: Why context shares data without prop drilling
MEDIUM IMPACT
This affects how data is passed through components, impacting rendering speed and DOM updates by reducing unnecessary re-renders.
Parent.svelte: <script> import { setContext } from 'svelte'; import { writable } from 'svelte/store'; let user = writable({ name: 'Alice' }); setContext('user', user); </script> <Child1 /> Child1.svelte: <script> import { getContext } from 'svelte'; const user = getContext('user'); </script> <Child2 /> Child2.svelte: <script> import { getContext } from 'svelte'; const user = getContext('user'); </script> <p>{$user.name}</p>
Parent.svelte: <script> let user = { name: 'Alice' }; </script> <Child1 user={user} /> Child1.svelte: <script> export let user; </script> <Child2 user={user} /> Child2.svelte: <script> export let user; </script> <p>{user.name}</p>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Prop Drilling | Multiple prop passes through components | Multiple reflows due to repeated updates | Higher paint cost from unnecessary renders | [X] Bad |
| Context API | Direct data access in consumers only | Single reflow per data change | Lower paint cost with fewer renders | [OK] Good |