0
0
Svelteframework~8 mins

Context vs stores decision in Svelte - Performance Comparison

Choose your learning style9 modes available
Performance: Context vs stores decision
MEDIUM IMPACT
This affects how state sharing impacts page load speed and interaction responsiveness in Svelte apps.
Sharing reactive state across multiple unrelated components
Svelte
Using a Svelte store for global reactive state:

// userStore.js
import { writable } from 'svelte/store';
export const user = writable(null);

// AnyComponent.svelte
<script>
  import { user } from './userStore.js';

  user.subscribe(value => { /* react to changes */ });
</script>
Stores provide a centralized reactive state with efficient subscription management, minimizing unnecessary updates and improving responsiveness.
📈 Performance GainReduces re-renders to only subscribed components, improving INP and CPU usage.
Sharing reactive state across multiple unrelated components
Svelte
Using context to pass state deeply and across many components manually:

// Parent.svelte
<script>
  import { setContext } from 'svelte';
  import { user } from './userStore.js';
  setContext('user', user);
</script>

// DeepChild.svelte
<script>
  import { getContext } from 'svelte';
  const user = getContext('user');
  user.subscribe(value => { /* react to changes */ });
</script>
Context is designed for passing data down a component tree, not for global reactive state. Using it globally causes many components to re-render unnecessarily and complicates updates.
📉 Performance CostTriggers multiple re-renders and reactive updates across unrelated components, increasing INP and CPU usage.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Using context for global reactive stateHigh due to many subscriptionsMultiple reflows from unnecessary updatesHigh paint cost from frequent re-renders[X] Bad
Using stores for global reactive stateOptimized subscriptions only where neededMinimal reflows limited to subscribed componentsLower paint cost due to selective updates[OK] Good
Using stores for static dataUnnecessary subscription overheadUnneeded reflows on rare changesModerate paint cost[!] OK
Using context for static dataNo subscriptions, minimal DOM opsNo reflows triggered by data changesMinimal paint cost[OK] Good
Rendering Pipeline
Context and stores affect the reactive update phase of rendering. Stores trigger reactive subscriptions causing component updates, while context provides static or scoped data without reactive triggers unless combined with stores.
Reactive Update
Layout
Paint
⚠️ BottleneckReactive Update stage due to subscription notifications and component re-renders.
Core Web Vital Affected
INP
This affects how state sharing impacts page load speed and interaction responsiveness in Svelte apps.
Optimization Tips
1Use stores for global reactive state to minimize unnecessary component updates.
2Use context for static or scoped data to avoid subscription overhead.
3Avoid passing reactive state via context globally to reduce re-renders.
Performance Quiz - 3 Questions
Test your performance knowledge
Which pattern is best for sharing global reactive state in Svelte for optimal performance?
AUsing Svelte stores
BUsing context for all state
CPassing props deeply
DUsing global variables
DevTools: Performance
How to check: Record a performance profile while interacting with components using context or stores. Look for scripting and rendering times related to reactive updates.
What to look for: High scripting time and frequent component re-renders indicate inefficient state sharing. Efficient patterns show fewer reactive updates and lower CPU usage.