Skip to main content
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
1
  • Choose unique keys - Use item.id, not array index
  • Use primitives - Strings/numbers are better than objects
  • Keep keys stable - Don’t recreate keys on each render
  • Avoid Index as Key

    Minimize Component Re-renders

    Prevent unnecessary work by isolating reactive dependencies:

    Component Design Patterns

    Extract Expensive Logic

    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

    Use Modern Formats

    Profiling and Measurement

    Use Browser DevTools

    1
  • Open Chrome DevTools Performance tab
  • Record interaction (click, scroll, etc.)
  • Analyze flame graph for slow operations
  • Look for long tasks and excessive re-renders
  • Use $inspect.trace

    Debug reactive dependencies:

    Best Practices Checklist

    1
  • Use keyed {#each} blocks for all lists
  • Avoid $state for constants - use plain variables
  • Prefer $derived over $effect for computed values
  • Use $state.raw for large objects that are only reassigned
  • Lazy load large components with dynamic imports
  • Debounce user input for expensive operations
  • Profile before optimizing - measure, don’t guess
  • Extract expensive logic to dedicated components
  • Use virtual lists for >1000 items
  • Optimize images with lazy loading and modern formats
  • Common Performance Pitfalls

    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.