Performance: tick function
MEDIUM IMPACT
The tick function affects how updates are batched and when the DOM reflects state changes, impacting rendering speed and interaction smoothness.
import { tick } from 'svelte'; let count = 0; async function increment() { count++; await tick(); const height = document.getElementById('box').offsetHeight; console.log(height); }
import { afterUpdate } from 'svelte'; let count = 0; function increment() { count++; // Immediately query DOM here const height = document.getElementById('box').offsetHeight; console.log(height); } // Using afterUpdate causes code to run after every update, not just this one
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Using afterUpdate for DOM reads | Multiple reads after every update | Multiple forced reflows | High paint cost due to layout thrashing | [X] Bad |
| Using tick to await DOM update | Single read after update | Single reflow per update | Lower paint cost, smoother rendering | [OK] Good |