Svelte is fast by default, but understanding performance optimization techniques helps you build even faster applications.
Reactivity Optimization
Use $state Sparingly
Only make variables reactive when necessary:
Reactive state has overhead. Use plain variables for constants and values that never change.
Choose Between $state and $state.raw
For large objects that are only reassigned (not mutated), use $state.raw:
Use $state.raw when:
- Working with large API responses
- Data is replaced wholesale, not mutated
- Objects are frequently reassigned
Use $state when:
- Mutating nested properties (
user.name = 'Alice')
- Need fine-grained reactivity
- Working with small objects
Prefer $derived over $effect
Compute values with $derived, not $effect:
$derived is more efficient and clearer than $effect for computing values from state.
Avoid Reactive Statement Overhead
In legacy mode, reactive statements ($:) run more often than necessary:
Always use runes mode ($state, $derived, $effect) for better performance. Avoid legacy reactive statements.
Rendering Optimization
Use Keyed Each Blocks
Always provide keys in {#each} blocks for efficient updates:
Performance impact:
- Without key: O(n) - Updates all existing DOM nodes
- With key: O(log n) - Moves/inserts/removes specific nodes
Avoid Index as Key
Minimize Component Re-renders
Prevent unnecessary work by isolating reactive dependencies:
Component Design Patterns
Move expensive computations to dedicated components:
Use Event Delegation
For many similar elements, use event delegation:
Avoid Inline Object/Array Creation
Bundle Size Optimization
Code Splitting with Dynamic Imports
Lazy load components that aren’t immediately needed:
Tree Shaking
Import only what you need:
Analyze Bundle Size
Use tools to find optimization opportunities:
Advanced Optimizations
Virtual Lists for Large Datasets
For thousands of items, render only visible rows:
Debounce Expensive Operations
Memoize Complex Computations
Image and Asset Optimization
Lazy Load Images
Profiling and Measurement
Use $inspect.trace
Debug reactive dependencies:
Best Practices Checklist
Avoid these patterns:
- Using array index as
{#each} key
- Creating objects/arrays in templates:
style={{ color }}
- Making everything
$state “just in case”
- Using
$effect for derived values
- Not profiling before optimizing
- Premature optimization without measurements
Svelte is already very fast. Focus on correctness first, then optimize bottlenecks identified through profiling.