Performance: Checkbox and radio bindings
This affects interaction responsiveness and rendering speed when users toggle checkboxes or radio buttons bound to state.
let selected = false; <input type="checkbox" bind:checked={selected}>
let selected = false; function toggle() { selected = !selected; // manually update DOM or trigger extra logic } <input type="checkbox" on:change={toggle} checked={selected}>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual event handler with checked attribute | Multiple DOM updates | 2 reflows per toggle | Higher paint cost due to layout thrashing | [X] Bad |
| Svelte bind:checked directive | Single DOM update | 1 reflow per toggle | Lower paint cost with smooth input response | [OK] Good |
bind:checked to track true/false state.bind:value is for input values, bind:group is for radio or multiple checkboxes, bind:selected is invalid.color in Svelte?bind:group to bind multiple inputs to one variable.bind:group={color} correctly binds the group to variable color.<script>
let selectedColors = [];
</script>
<input type='checkbox' bind:group={selectedColors} value='red' /> Red
<input type='checkbox' bind:group={selectedColors} value='blue' /> Blue
<input type='checkbox' bind:group={selectedColors} value='green' /> Green
<p>Selected: {JSON.stringify(selectedColors)}</p>bind:group stores selected values in an array.<script>
let fruit = 'apple';
</script>
<input type='radio' bind:checked={fruit} value='apple' /> Apple
<input type='radio' bind:group={fruit} value='banana' /> Banana
bind:checked, the other uses bind:group on the same variable.bind:group to bind to one variable consistently.selectedToppings and the size to a string pizzaSize?bind:group on checkboxes to bind selected values to an array variable.bind:group on radio buttons to bind the selected value to a string variable.<script>
let selectedToppings = [];
let pizzaSize = '';
</script>
<input type='checkbox' bind:group={selectedToppings} value='pepperoni' /> Pepperoni
<input type='checkbox' bind:group={selectedToppings} value='mushrooms' /> Mushrooms
<input type='radio' bind:group={pizzaSize} value='small' /> Small
<input type='radio' bind:group={pizzaSize} value='large' /> Large correctly uses bind:group for both checkboxes and radios with proper variables.Pass the test and this pattern moves unknown