0
0
Svelteframework~8 mins

Auto-subscription with $ prefix in Svelte - Performance & Optimization

Choose your learning style9 modes available
Performance: Auto-subscription with $ prefix
MEDIUM IMPACT
This affects how reactive updates trigger DOM changes and browser rendering performance.
Reactively updating UI when a store value changes
Svelte
import { writable } from 'svelte/store';
const count = writable(0);

// In component template or script
// Use $count directly to auto-subscribe and update UI reactively

<p>{$count}</p>
Auto-subscription with $ prefix lets Svelte track dependencies and update UI efficiently without manual subscription code.
📈 Performance GainSingle reactivity update per change; avoids redundant reflows and memory leaks
Reactively updating UI when a store value changes
Svelte
import { writable } from 'svelte/store';
const count = writable(0);

let currentCount;
const unsubscribe = count.subscribe(value => {
  currentCount = value;
  // manually update UI or trigger re-render
});

// Use currentCount in template

// Remember to unsubscribe when no longer needed
// unsubscribe();
Manually subscribing requires extra code and can cause unnecessary re-renders or memory leaks if unsubscribed incorrectly.
📉 Performance CostTriggers multiple reflows if subscription updates cause repeated DOM writes; manual subscription overhead
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Manual subscription with subscribe()Multiple DOM writes if not batchedMultiple reflows per updateHigher paint cost due to repeated updates[X] Bad
Auto-subscription with $ prefixSingle DOM write per reactive changeSingle reflow per updateLower paint cost due to efficient batching[OK] Good
Rendering Pipeline
When a store value changes, the $ prefix auto-subscription triggers Svelte's reactive system to schedule updates. This updates the virtual DOM and then the real DOM efficiently.
Dependency Tracking
Update Scheduling
DOM Update
Paint
⚠️ BottleneckDOM Update stage if many reactive updates happen simultaneously
Core Web Vital Affected
INP
This affects how reactive updates trigger DOM changes and browser rendering performance.
Optimization Tips
1Use $ prefix to auto-subscribe to stores for efficient reactive updates.
2Avoid manual subscriptions unless necessary to prevent memory leaks.
3Batch updates to minimize layout thrashing and reflows.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of using the $ prefix for store values in Svelte?
AIt prevents any DOM updates from happening.
BIt delays updates to reduce CPU usage.
CIt automatically subscribes and updates UI efficiently without manual code.
DIt bundles store code separately to reduce file size.
DevTools: Performance
How to check: Record a performance profile while interacting with the component. Look for the number of style recalculations and layout events triggered by store updates.
What to look for: Fewer layout recalculations and shorter scripting times indicate efficient reactive updates with $ prefix.