This approach turns your code into efficient JavaScript before running it. It skips the extra step of comparing changes in a virtual copy of the page, making updates faster and simpler.
Compiler-based approach (no virtual DOM) in Svelte
No special syntax in your code; the Svelte compiler automatically converts your components into efficient JavaScript that updates the real DOM directly.<script> let count = 0; function increment() { count += 1; } </script> <button on:click={increment}> Clicked {count} {count === 1 ? 'time' : 'times'} </button>
name changes, without using a virtual DOM.<script> let name = 'Friend'; </script> <h1>Hello {name}!</h1>
This simple Svelte component shows a button with a message. Each click updates the message directly on the page. The compiler creates JavaScript that changes only the text inside the button, without using a virtual DOM.
It also includes an ARIA label for accessibility.
<script> let message = 'Hello'; let count = 0; function update() { count += 1; message = `Clicked ${count} times`; } </script> <button on:click={update} aria-label="Click to update message"> {message} </button>
Svelte's compiler approach means less code runs in the browser, improving speed and battery life.
Because it updates the real DOM directly, debugging is easier--you see exactly what your code does.
This approach is different from frameworks that keep a virtual copy of the page and compare changes before updating.
Svelte compiles your code into efficient JavaScript that updates the real page directly.
This avoids the extra step of using a virtual DOM, making apps faster and simpler.
It helps with performance, smaller file sizes, and easier debugging.