Performance: Why advanced features enable production apps
Advanced features in Svelte improve app load speed and runtime efficiency by reducing unnecessary work and optimizing rendering.
<script> import { onMount } from 'svelte'; let data; onMount(async () => { data = await fetch('/api/data').then(r => r.json()); }); </script> {#if data} <p>{data.message}</p> {/if} // Using reactive declarations and built-in Svelte features
import { onMount } from 'svelte'; onMount(() => { fetch('/api/data').then(res => res.json()).then(json => { document.querySelector('#data').textContent = json.message; }); }); // Using manual DOM updates and no reactive declarations
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual DOM updates without reactivity | High (many nodes updated) | Multiple reflows | High paint cost | [X] Bad |
| Svelte reactive declarations and lifecycle | Minimal DOM updates | Single reflow | Low paint cost | [OK] Good |
$: automatically run when their dependencies change, updating the UI.bind:this references DOM nodes, createEventDispatcher creates events, and on:click handles clicks, but none auto-update UI on data change.bind:this updates UI automatically$: before the statement to auto-update when dependencies change.let count = 0; is normal variable declaration, const and function do not create reactive variables.count is 1, so doubled = 2. The console.log runs once during setup, logging 2.count to 3 triggers the reactive statement again, updating doubled to 6 and logging it again.count++}>Increment{doubled}
$: doubled = count * 2 is valid without a semicolon.count, which triggers the reactive statement to update doubled and display it correctly.each blocks with keys lets Svelte track items and update only changed elements, minimizing DOM changes.Pass the test and this pattern moves unknown