RAF vs setTimeout
RAF vs setTimeout
Section titled “RAF vs setTimeout”Introduction
Section titled “Introduction”While you can animate with setTimeout/setInterval, requestAnimationFrame provides significant advantages for visual updates.
Comparison
Section titled “Comparison”// setTimeout animation (choppy)let pos = 0;setInterval(() => { pos += 1; element.style.left = pos + 'px';}, 16); // May not align with frame rate
// requestAnimationFrame (smooth)function smoothAnimation() { pos += 1; element.style.left = pos + 'px'; requestAnimationFrame(smoothAnimation);}requestAnimationFrame(smoothAnimation);Key Differences
Section titled “Key Differences”| Feature | requestAnimationFrame | setTimeout |
|---|---|---|
| Syncs with monitor | ✅ Yes | ❌ No |
| Pauses in background | ✅ Yes | ❌ No |
| Frame skipping | ✅ Auto-catches up | ❌ Can queue up |
| Battery friendly | ✅ Yes | ❌ No |
| Precision | ~16ms (60fps) | Minimum 4ms |
Benefits of RAF
Section titled “Benefits of RAF”- ✅ Syncs with monitor refresh rate
- ✅ Pauses when tab is in background (saves battery)
- ✅ Smoother animations (no frame drops)
- ✅ Browser can batch updates for performance
- ✅ Auto-throttles to match display capabilities
Summary
Section titled “Summary”- Never use
setTimeout/setIntervalfor animations requestAnimationFrameis purpose-built for visuals- Automatically pauses in background tabs
- Browser optimizes for best performance