Performance: Why events drive user interaction
HIGH IMPACT
This concept affects how quickly the page responds to user actions and how smoothly interactions feel.
function handleClick() { requestAnimationFrame(() => { console.log('Clicked'); }); } document.querySelectorAll('button').forEach(btn => { btn.addEventListener('click', handleClick); });
document.querySelectorAll('button').forEach(btn => { btn.addEventListener('click', () => { // heavy logic for(let i=0; i<1000000; i++) {} console.log('Clicked'); }); });
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Heavy synchronous event handler | Minimal | Triggers 1 reflow if layout changes | High paint cost due to blocking | [X] Bad |
| Lightweight event handler with deferred work | Minimal | Single reflow if needed | Low paint cost, smooth interaction | [OK] Good |