Throttling in JavaScript limits a function so it runs at most once per time interval, no matter how often the event that triggers it fires. Use it for high-frequency events such as scroll, resize and pointermove, where you want regular updates but not thousands of calls per second. Debouncing is different: it waits until the events stop before running once.
A throttle is a small wrapper function around a timestamp or a timer. This guide shows leading and trailing versions, how to use them with the browser, and when a modern API such as requestAnimationFrame or IntersectionObserver is a better choice.
Both techniques control how often a handler runs, but they answer different questions.
| Technique | Behaviour | Typical use |
|---|---|---|
| Throttle | Runs at most once every N milliseconds while events continue | Scroll position, resize, drag, mouse move, rate-limited API calls |
| Debounce | Runs once, N milliseconds after the last event | Search-as-you-type, auto-save, form validation |
If you hold a key for two seconds with a 500 ms throttle, the handler runs about four times. With a 500 ms debounce, it runs once, half a second after you release the key.
The simplest version runs the function immediately and then ignores calls until the interval has passed.
performance.now() is monotonic, so it is not affected by system clock changes the way Date.now() can be. A regular function is used for the returned wrapper so that this is the element that received the event. The { passive: true } option tells the browser the handler will not call preventDefault(), which lets scrolling stay smooth.
The weakness of this version is that the last event in a burst can be dropped. If the user stops scrolling during the quiet period, your handler never sees the final position.
A trailing call runs once more after the interval, with the latest arguments, so the final state is never lost.
The cancel method matters in components. Call it when an element is removed or a component unmounts, otherwise a pending timer can fire after the page state is gone.
If you do not want to maintain this yourself, Lodash provides throttle with the same leading and trailing options, and it is well tested.
Throttling on the client reduces traffic, but it does not replace server-side rate limiting, because a client can ignore your script.
For a button that must not fire twice, throttling is the wrong tool because it still allows a second call after the delay. Disable the button while the request is running.
Throttling by time is a blunt instrument. The browser offers more precise tools for common cases.
requestAnimationFrame: runs your code once per frame, which is the right rate for animations and visual updates that follow scrolling or pointer movement.IntersectionObserver: reports when an element enters or leaves the viewport, so you do not need scroll handlers for lazy loading or reveal effects.ResizeObserver: reports element size changes without listening to window.resize.scroll-snap, position: sticky and scroll-driven animations: handle many effects with no JavaScript at all.Use time-based throttling for work that is not tied to rendering, such as network calls, analytics events or expensive calculations.
requestAnimationFrame.removeEventListener works, and call cancel on cleanup.A throttle runs repeatedly at a fixed maximum rate while events continue. A debounce runs once after the events have stopped for a set time.
Usually no. Debounce is the better fit, because you want one request after the user pauses typing.
For visual updates, requestAnimationFrame is better because it matches the screen refresh. For network calls and non-visual work, time-based throttling is appropriate.
No. Client-side throttling improves efficiency and user experience, but the server must still enforce its own limits.
Throttling caps how often a function runs during a stream of events. Write or import a throttle with leading and trailing options, create it once, clean it up, and prefer requestAnimationFrame or observer APIs for rendering-related work.