Performance: Input bindings (bind:value)
This affects interaction responsiveness and rendering speed when users type or change input fields.
<input type="text" bind:value={value} /> <script> let value = ''; </script>
<input type="text" on:input={handleInput} /> <script> let value = ''; function handleInput(event) { value = event.target.value; } </script>
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Manual on:input handler | 1 per input | 1 per keystroke | Moderate | [!] OK |
| bind:value two-way binding | 1 per input | 1 per keystroke | Low to moderate | [OK] Good |
| bind:value on many inputs without batching | Many | Many per keystroke | High | [X] Bad |
| bind:value with debounced updates | Many | Few | Low | [OK] Good |
bind:value do in a Svelte input element?bind:value connects the input's value to a variable so changes in one update the other automatically.name to an input's value in Svelte?bind:value={variable}.<input bind:value={name} /> matches the correct syntax. <input value={name} /> only sets initial value without binding. Options B and C use incorrect binding syntax.<script>
let text = '';
</script>
<input bind:value={text} />
<p>You typed: {text}</p>text, so typing updates text immediately.You typed: {text}, which updates to the current input value, here "Hello".<script> let age = 30; </script> <input bind:value=age />
bind:value={age}.firstName and lastName. You also want to show the full name updating live as the user types. Which Svelte code correctly achieves this?<script> let firstName = ''; let lastName = ''; </script> <!-- Options below -->
bind:value={firstName} and bind:value={lastName} to keep inputs synced with variables.{firstName} {lastName}, which updates live. <input bind:value={firstName} placeholder="First" />
<input bind:value={lastName} placeholder="Last" />
<p>Full name: {firstName + lastName}</p> concatenates without space, which is less readable. Options B and C have incorrect bindings.Pass the test and this pattern moves unknown