Performance: DOM event listeners (on:click)
MEDIUM IMPACT
This affects interaction responsiveness and memory usage by managing how click events are handled in the browser.
<script> let items = Array(1000).fill(0); function handleClick(event) { const index = event.target.dataset.index; if (index !== undefined) console.log(index); } </script> <div on:click={handleClick}> {#each items as item, i} <button data-index={i}>Click {i}</button> {/each} </div>
<script> let items = Array(1000).fill(0); </script> {#each items as item, i} <button on:click={() => console.log(i)}>Click {i}</button> {/each}
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Individual listeners per element | Many listeners attached (e.g., 1000) | 0 reflows triggered by listeners | Minimal paint cost | [X] Bad |
| Single delegated listener on parent | One listener attached | 0 reflows triggered by listener | Minimal paint cost | [OK] Good |