Performance: Keyed each blocks
This affects how efficiently the browser updates the DOM when lists change, impacting rendering speed and visual stability.
<script> let items = [1, 2, 3]; </script> {#each items as item (item)} <div>{item}</div> {/each}
<script> let items = [1, 2, 3]; </script> {#each items as item} <div>{item}</div> {/each}
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Unkeyed each block | Recreates or reorders many DOM nodes | Multiple reflows per update | High paint cost due to layout shifts | [X] Bad |
| Keyed each block | Updates only changed DOM nodes | Single or minimal reflows | Low paint cost with stable layout | [OK] Good |
keyed each block in Svelte?{#each items as item (item.id)} matches the correct syntax exactly, others miss parentheses or order.{#each users as user (user.id)}
{user.name}
{/each}users array changes by adding a new user with a unique id?{#each items as item (item.key)}
{item.value}
{/each}item.key is not unique for all items, what problem can occur?{#each tasks as task (???)}
{task.name}
{/each}Pass the test and this pattern moves unknown