0
0
Svelteframework~8 mins

Context with stores in Svelte - Performance & Optimization

Choose your learning style9 modes available
Performance: Context with stores
MEDIUM IMPACT
This affects how efficiently state is shared and updated across components without unnecessary DOM updates or re-renders.
Sharing reactive state across multiple nested components
Svelte
import { writable } from 'svelte/store';
import { setContext, getContext } from 'svelte';

const count = writable(0);
setContext('countStore', count);

// In nested components
const count = getContext('countStore');
Using context avoids prop drilling and limits re-renders to components that actually use the store.
📈 Performance Gainreduces re-renders to only subscribed components, improving input responsiveness
Sharing reactive state across multiple nested components
Svelte
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
Passing stores through many props causes unnecessary re-renders and prop drilling, increasing DOM updates and slowing interaction.
📉 Performance Costtriggers multiple re-renders and prop updates for each intermediate component
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Passing stores via props through many componentsMany prop updates and component re-rendersMultiple reflows triggered by each updateHigher paint cost due to repeated updates[X] Bad
Providing stores via Svelte contextOnly subscribed components updateSingle reflow per store updateLower paint cost due to targeted updates[OK] Good
Rendering Pipeline
When a store value changes, only components subscribed to that store update. Using context to provide the store avoids unnecessary prop updates and reflows in intermediate components.
JavaScript Execution
Style Calculation
Layout
Paint
⚠️ BottleneckJavaScript Execution due to unnecessary component updates
Core Web Vital Affected
INP
This affects how efficiently state is shared and updated across components without unnecessary DOM updates or re-renders.
Optimization Tips
1Use Svelte context to provide stores to avoid prop drilling.
2Subscribe only in components that need the store to limit updates.
3Avoid passing stores through many intermediate components to reduce re-renders.
Performance Quiz - 3 Questions
Test your performance knowledge
What is a main performance benefit of using Svelte context to provide stores instead of passing them as props?
AIt increases the bundle size significantly.
BIt reduces unnecessary component re-renders by avoiding prop drilling.
CIt triggers more layout recalculations.
DIt disables reactive updates in components.
DevTools: Performance
How to check: Record a performance profile while interacting with the app. Look for excessive component updates or re-renders in the flame chart.
What to look for: Fewer component re-renders and shorter scripting times indicate better performance with context stores.