Performance: setContext
MEDIUM IMPACT
This affects how data is shared between components without prop drilling, impacting rendering and update efficiency.
<Parent>
{#if true}
<Child />
{/if}
<script>
import { setContext } from 'svelte';
setContext('key', data);
</script>
</Parent>
<Child>
<GrandChild />
<script>
import { getContext } from 'svelte';
const data = getContext('key');
</script>
</Child><Parent>
<Child prop={data} />
</Parent>
<Child>
<GrandChild prop={prop} />
</Child>| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Prop drilling through many components | High (many props passed) | Multiple reflows per intermediate update | Higher paint cost due to repeated updates | [X] Bad |
| Using setContext for data sharing | Low (direct context access) | Single reflow on update | Lower paint cost with fewer updates | [OK] Good |