Performance: Why components are the building blocks
This concept affects how efficiently the page loads and updates by controlling how much of the UI the browser must render or re-render.
<script> import Counter from './Counter.svelte'; </script> <div> <Counter /> <p>Other unrelated content</p> </div> <!-- Counter.svelte --> <script> let count = 0; </script> <button on:click={() => count++}>Click me</button> <p>{count}</p>
<script> let count = 0; </script> <div> <button on:click={() => count++}>Click me</button> <p>{count}</p> <p>Other unrelated content</p> </div>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Single large component | Many nodes updated on any change | Multiple reflows per update | High paint cost | [X] Bad |
| Small isolated components | Only relevant nodes updated | Single reflow per component update | Low paint cost | [OK] Good |
<script>
export let name = 'Friend';
</script>
<h1>Hello {name}!</h1><Greeting name="Anna" />?name with default 'Friend'. Passing name="Anna" overrides this default.{name} in the template will show 'Anna' because the prop was passed.<script>
let count = 0
function increment() {
count++
}
</script>
<button on:click="increment()">Click</button>
<p>Count: {count}</p>on:event={handler} without quotes around the handler.on:click="increment()" with quotes, which is incorrect and causes a syntax error.Pass the test and this pattern moves unknown