Performance: Why special elements handle edge cases
MEDIUM IMPACT
This concept affects how efficiently the browser updates the UI when special elements handle unusual or complex cases.
<script> let items = []; // Using keyed each block to update only changed items function updateItems(newItems) { items = newItems; } </script> <ul> {#each items as item (item.id)} <li>{item.name}</li> {/each} </ul>
<script> let items = []; // Updating entire list on any change function updateItems(newItems) { items = newItems; } </script> <ul> {#each items as item} <li>{item}</li> {/each} </ul>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Non-keyed each block | Updates all list items | Multiple reflows per update | High paint cost | [X] Bad |
| Keyed each block | Updates only changed items | Single or few reflows | Low paint cost | [OK] Good |