Performance: Script, markup, and style sections
This concept affects initial page load speed and rendering performance by how scripts, markup, and styles are processed and applied.
<script>let count = 0;</script> <div>{count}</div> <style>div { font-size: 1rem; }</style>
<script>import bigLib from 'big-lib'; let count = 0;</script> <div>{count}</div> <style>div { font-size: 16px; }</style>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Large script imports in <script> | Minimal | 1 (initial) | Medium (due to delayed paint) | [X] Bad |
| Minimal script with scoped styles | Minimal | 1 (initial) | Low | [OK] Good |
<script> section is for JavaScript logic, <style> is for CSS, and markup is the HTML structure.<script> section.<style> tag to the component without needing extra attributes.<style> with CSS inside. Adding scoped or global changes behavior or is invalid.<script>
let name = 'Anna';
</script>
<h1>Hello {name}!
</h1>
<style>
h1 { color: blue; }
</style>{name} inside markup inserts the value of the variable name, which is 'Anna'.<style> section styles the h1 with blue color scoped to this component.<script>
let count = 0
</script>
<button on:click="count++">Click me</button>
<style>
button { font-size: 1.2rem; }
</style>on:event={handler} without quotes around the handler expression.on:click="count++" with quotes, which is invalid syntax in Svelte.error is true. Which combination of script, markup, and style is correct?
<script>
let error = true;
</script>
<!-- markup here -->
<style>
.error { color: red; }
</style>{#if error} ... {/if} block conditionally renders content based on the variable.class="error" applies the scoped style .error { color: red; } correctly.Pass the test and this pattern moves unknown