Performance: If blocks ({#if})
Controls conditional rendering which impacts DOM size and layout recalculations during state changes.
{#if showContent}
<div>Content</div>
{/if} <!-- element only in DOM when needed --><div style="display: none">Content</div> <!-- always in DOM but hidden -->
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Using CSS display:none to hide elements | No DOM changes | No reflow triggered for hidden elements | Paint triggered for hidden elements | [X] Bad |
| Using {#if} to conditionally render elements | DOM nodes added/removed as needed | Single reflow per toggle | Paint only for visible elements | [OK] Good |
{#if} block do in Svelte?{#if} block in Svelte?{#if condition} ... {/if} to start and end an if block.{#if condition} ... {#end} uses a wrong closing tag.{`\n{#if loggedIn}\n Welcome back!
\n{:else}\n Please log in.
\n{/if}`}{`\n{#if count > 3}\n Count is high
\n{:else if count > 1}\n Count is medium
\n{:else}\n Count is low
\n{/if}`}{#if} block correctly checks both conditions?{#if loggedIn | isAdmin} Welcome, admin!
{/if} uses | bitwise OR which shows if either is true (wrong). {#if loggedIn || isAdmin} Welcome, admin!
{/if} uses || logical OR which means either can be true (wrong). {#if loggedIn} {:if isAdmin} Welcome, admin!
{/if} {/if} uses invalid {:if} syntax. {#if loggedIn && isAdmin} Welcome, admin!
{/if} uses && logical AND which requires both true (correct).Pass the test and this pattern moves unknown