JavaScript·Intermediate·Coding·1 min read
Is requestAnimationFrame a throttle?
Short interview answer
It can coalesce visual work to at most once per rendering frame, which is often better than an arbitrary millisecond interval for DOM reads and writes. It is not a general time-based throttle and pauses or slows in background tabs.
Example
function rafThrottle(fn) { let scheduled = false; return (...args) => { if (scheduled) return; scheduled = true; requestAnimationFrame(() => { scheduled = false; fn(...args); }); };}Key takeaway
Explain the underlying mental model clearly, then support it with a concrete example and its trade-offs.