0
0
Svelteframework~8 mins

Why context shares data without prop drilling in Svelte - Performance Evidence

Choose your learning style9 modes available
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.
Passing shared data deeply through nested components
Svelte
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>
Context shares data directly with components that need it, skipping intermediate layers and reducing unnecessary re-renders.
📈 Performance GainReduces re-renders and prop updates, improving INP and lowering CPU usage.
Passing shared data deeply through nested components
Svelte
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>
Passing props through many layers causes each intermediate component to re-render when data changes, even if they don't use the data.
📉 Performance CostTriggers multiple re-renders and prop updates, increasing INP and CPU usage.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Prop DrillingMultiple prop passes through componentsMultiple reflows due to repeated updatesHigher paint cost from unnecessary renders[X] Bad
Context APIDirect data access in consumers onlySingle reflow per data changeLower paint cost with fewer renders[OK] Good
Rendering Pipeline
When using prop drilling, each component in the chain processes props causing multiple style recalculations and layout passes. Context skips intermediate components, reducing these steps.
Style Calculation
Layout
Paint
⚠️ BottleneckLayout due to repeated reflows from prop updates in intermediate components
Core Web Vital Affected
INP
This affects how data is passed through components, impacting rendering speed and DOM updates by reducing unnecessary re-renders.
Optimization Tips
1Use context to share data directly with components that need it.
2Avoid passing props through many layers to reduce re-renders.
3Reducing unnecessary renders improves input responsiveness (INP).
Performance Quiz - 3 Questions
Test your performance knowledge
How does using context instead of prop drilling affect component re-renders?
AIt has no effect on re-renders.
BIt increases re-renders because context updates all components.
CIt reduces unnecessary re-renders by skipping intermediate components.
DIt causes more reflows due to extra DOM nodes.
DevTools: Performance
How to check: Record a performance profile while interacting with the app. Look for multiple component renders and layout recalculations in the flame chart.
What to look for: Fewer component renders and layout recalculations indicate better performance with context.