Performance: Dimension bindings (clientWidth, clientHeight)
MEDIUM IMPACT
This affects page responsiveness and layout stability by triggering layout recalculations when reading element dimensions.
import { onMount } from 'svelte'; let width; let box; onMount(() => { width = box.clientWidth; window.addEventListener('resize', () => { width = box.clientWidth; }); }); // Use a reference and event listener to limit reads to resize events
let width; $: width = document.getElementById('box').clientWidth; // This runs on every reactive update, forcing layout thrashing
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Reading clientWidth in reactive statement every update | Multiple DOM reads | Multiple reflows triggered | High paint cost due to layout thrashing | [X] Bad |
| Reading clientWidth on mount and resize event only | Few DOM reads | Single reflow per resize | Low paint cost, smooth rendering | [OK] Good |