Performance: Form validation
MEDIUM IMPACT
Form validation affects the interaction responsiveness and visual stability during user input and form submission.
import { tick } from 'svelte'; let error = ''; let timeout; function validateInputDebounced(value) { clearTimeout(timeout); timeout = setTimeout(() => { if (value.length < 3) { error = 'Too short'; } else { error = ''; } }, 300); } <input type="text" on:input={e => validateInputDebounced(e.target.value)} bind:value={inputValue} /> {#if error} <p>{error}</p> {/if}
let error = ''; function validateInput(value) { if (value.length < 3) { error = 'Too short'; } else { error = ''; } } <input type="text" on:input={e => validateInput(e.target.value)} bind:value={inputValue} /> {#if error} <p>{error}</p> {/if}
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Validate on every keystroke | Many state updates and DOM changes | Multiple reflows per input | High paint cost due to frequent updates | [X] Bad |
| Debounced validation | Fewer state updates, minimal DOM changes | Single reflow after debounce | Lower paint cost | [OK] Good |
| Add/remove error nodes | DOM nodes added/removed frequently | Reflows and layout shifts | High paint cost and CLS impact | [X] Bad |
| Toggle error visibility | DOM nodes remain, only style changes | Minimal reflows, no layout shift | Low paint cost and stable layout | [OK] Good |