Performance: Element reference bindings (bind:this)
MEDIUM IMPACT
This affects how quickly the browser can access and manipulate DOM elements after initial render, impacting interaction responsiveness.
let inputElement; function focusInput() { inputElement.focus(); } <input bind:this={inputElement} /> <button on:click={focusInput}>Focus</button>
let inputElement; function focusInput() { inputElement = document.querySelector('#myInput'); inputElement.focus(); } <input id="myInput" /> <button on:click={focusInput}>Focus</button>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Using document.querySelector repeatedly | Multiple DOM queries | Triggers 1 reflow per query | Moderate paint cost due to layout recalculation | [X] Bad |
| Using bind:this once to store element reference | Single DOM reference assignment | No reflows on access | Minimal paint cost | [OK] Good |