Performance: Group bindings
Group bindings affect how efficiently the framework updates multiple related inputs and their shared state, impacting interaction responsiveness and layout stability.
let selected = []; <input type="checkbox" bind:group={selected} value="a"> <input type="checkbox" bind:group={selected} value="b"> <input type="checkbox" bind:group={selected} value="c">
let selected = []; function toggle(item) { if (selected.includes(item)) { selected = selected.filter(i => i !== item); } else { selected = [...selected, item]; } } <input type="checkbox" on:change={() => toggle('a')} checked={selected.includes('a')}> <input type="checkbox" on:change={() => toggle('b')} checked={selected.includes('b')}> <input type="checkbox" on:change={() => toggle('c')} checked={selected.includes('c')}>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Individual checkbox event handlers | Multiple event listeners, multiple state updates | Multiple reflows per toggle | Higher paint cost due to repeated updates | [X] Bad |
| Svelte group binding on checkboxes | Single shared event binding, one state update | Single reflow per toggle | Lower paint cost with batched updates | [OK] Good |
selected in Svelte?bind:group to connect all radios to one variable.bind:group={selected}, which is the correct syntax for radio groups.<script>
let selectedFruits = [];
</script>
<label><input type="checkbox" bind:group={selectedFruits} value="apple"> Apple</label>
<label><input type="checkbox" bind:group={selectedFruits} value="banana"> Banana</label>
<label><input type="checkbox" bind:group={selectedFruits} value="cherry"> Cherry</label>
<p>Selected: {JSON.stringify(selectedFruits)}</p><script>
let selected = "";
</script>
<input type="checkbox" bind:group={selected} value="x">
<input type="checkbox" bind:group={selected} value="y">
colors and shape respectively?bind:group with an array variable; radios use bind:group with a string variable.Pass the test and this pattern moves unknown