The following is a guest post by David Corbacho, a front end engineer in London. We’ve broached this topic before, but this time, David is going to drive the concepts home through interactive demos that make things very clear.
Debounce and throttle are two similar (but different!) techniques to control how many times we allow a function to be executed over time.
Having a debounced or throttled version of our function is especially useful when we are attaching the function to a DOM event. Why? Because we are giving ourselves a layer of control between the event and the execution of the function. Remember, we don’t control how often those DOM events are going to be emitted. It can vary.
For example, let’s talk about scroll events. See this example:
See the Pen Scroll events counter by Corbacho (@dcorb) on CodePen.
When scrolling using a trackpad, scroll wheel, or just by dragging a scrollbar can trigger easily 30 events per second. But scrolling slowly (swapping) in a smartphone could trigger as much as 100 events per second during my tests. Is your scroll handler prepared for this rate of execution?
In 2011, an issue popped up on the Twitter website: when you were scrolling down your Twitter feed, it became slow and unresponsive. John Resig published a blog post about the problem where it was explained how bad of an idea it is to directly attach expensive functions to the scroll
event.
The suggested solution by John (at that time, five years ago) was a loop running every 250ms, outside of the onScroll event
. That way the handler is not coupled to the event. With this simple technique, we can avoid ruining the user experience.
These days there are slightly more sophisticated ways of handling events. Let me introduce you to Debounce, Throttle, and requestAnimationFrame. We’ll also look at the matching use cases.
Debounce
The Debounce technique allow us to “group” multiple sequential calls in a single one.

Imagine you are in an elevator. The doors begin to close, and suddenly another person tries to get on. The elevator doesn’t begin its function to change floors, the doors open again. Now it happens again with another person. The elevator is delaying its function (moving floors), but optimizing its resources.
Try it for yourself. Click or move the mouse on top of the button:
See the Pen Debounce. Trailing by Corbacho (@dcorb) on CodePen.
You can see how sequential fast events are represented by a single debounced event. But if the events are triggered with big gaps, the debouncing doesn’t happen.
Leading edge (or “immediate”)
You may find it irritating that the debouncing event waits before triggering the function execution, until the events stop happening so rapidly. Why not trigger the function execution immediately, so it behaves exactly as the original non-debounced handler? But not fire again until there is a pause in the rapid calls.
You can do this! Here’s an example with the leading
flag on:

In underscore.js, the option is called immediate
instead of leading
Try it for yourself:
See the Pen Debounce. Leading by Corbacho (@dcorb) on CodePen.
Debounce Implementations
The first time I saw debounce implemented in JavaScript was in 2009 in this John Hann post (who also coined the term).
Soon after that, Ben Alman created a jQuery plugin (no longer maintained), and a year after, Jeremy Ashkenas