Performance: Why template syntax renders dynamic content
This affects how quickly dynamic content appears on the page and how efficiently the browser updates the UI when data changes.
<script> let count = 0; </script> <p>{count}</p> <button on:click={() => count++}>Increment</button>
<script> let count = 0; </script> <p>{count}</p> <button on:click={() => { count++; document.querySelector('p').textContent = count; }}>Increment</button>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual DOM update on event | Multiple direct DOM writes | Triggers reflow on each update | High paint cost due to forced layout | [X] Bad |
| Svelte template reactive update | Minimal DOM updates via reactive bindings | Single reflow per change | Low paint cost with efficient diffing | [OK] Good |
{ } inside the template?{ } are used to embed JavaScript expressions or variables inside the HTML markup so that their values appear dynamically.name inside a Svelte template?{ } to insert variables inside HTML.<p>{name}</p> uses {name}, which is correct. Options A, C, and D use invalid or incorrect syntax for Svelte. <script>
let count = 3;
</script>
<p>Count is {count} now.</p>count is set to 3 in the script.{count} which inserts the value 3 into the paragraph. <script>
let message = 'Hello';
</script>
<p>{message</p>{ is present but the closing brace } after message is missing.firstName and lastName. Which template syntax correctly renders the full name with a space between them?+ concatenates strings correctly.<p>{firstName + ' ' + lastName}</p> uses firstName + ' ' + lastName, which is valid. <p>{firstName, ' ', lastName}</p> uses commas which do not concatenate strings. <p>{firstName & ' ' & lastName}</p> uses & which is invalid in JS. <p>{firstName.concat(' ', lastName)}</p> uses concat method correctly with closing parenthesis.Pass the test and this pattern moves unknown