Performance: Common action patterns (click-outside, tooltip)
MEDIUM IMPACT
These patterns affect interaction responsiveness and visual stability by managing event listeners and DOM updates efficiently.
import { onMount, onDestroy } from 'svelte'; onMount(() => { const handleClick = event => { if (!element.contains(event.target)) { closeTooltip(); } }; window.addEventListener('click', handleClick); onDestroy(() => window.removeEventListener('click', handleClick)); });
window.addEventListener('click', (event) => { if (!element.contains(event.target)) { closeTooltip(); } });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Global click listener without cleanup | Multiple listeners added | Multiple reflows on event | High paint cost on updates | [X] Bad |
| Global click listener with cleanup | Single listener active | Single reflow per event | Moderate paint cost | [OK] Good |
| Tooltip shown via JS reactivity | DOM nodes added/removed | Reflow and repaint on hover | Moderate paint cost | [!] OK |
| Tooltip shown via CSS hover | No DOM changes | No reflows on hover | Low paint cost | [OK] Good |