Debounce & Throttle
Learn what JavaScript debounce and throttle are, how they differ, and how to control search input, scroll and resize events.
On this page
1. What are debounce and throttle?
Both techniques are for events that fire very fast, such as typing, scroll, resize and mouse move. Both wrap a function and return a new one.
2. Why do they matter?
If a user types "javascript" in a search box, that is 10 keystrokes. If every keystroke makes an API call, 10 requests go out, when you only need the last one.
| Without control | With debounce or throttle |
|---|---|
| An API call on every keystroke | Just one call when typing stops |
| Over 100 handler calls per second on scroll | One call every 200ms |
| Wasted load on the server | Fewer requests, lower cost |
| The UI can lag | A smooth UI |
3. Where are they used?
| Situation | Technique | Reason |
|---|---|---|
| Search suggestions | Debounce | Search only after typing stops |
| Form auto save | Debounce | Save when the user pauses |
| Window resize | Debounce | Calculate the layout for the final size |
| Infinite scroll | Throttle | Regular checks while scrolling |
| Mouse move tracking | Throttle | Update the position at a fixed rate |
| Button double click | Throttle | Only one submit at a time |
4. How do they work?
Both have a closure inside that remembers the timer or the time of the last call.
An easy example: debounce is an elevator door that waits again for every new person. Throttle is a bus that leaves every 10 minutes, no matter how many people show up.
5. Code example
Debounce
function debounce(fn, delay = 300) {
let timerId;
return (...args) => {
clearTimeout(timerId);
timerId = setTimeout(() => fn(...args), delay);
};
}
const search = debounce((query) => {
console.log("API call for:", query);
}, 400);
input.addEventListener("input", (e) => search(e.target.value));Throttle
function throttle(fn, interval = 200) {
let lastRun = 0;
return (...args) => {
const now = Date.now();
if (now - lastRun >= interval) {
lastRun = now;
fn(...args);
}
};
}
const onScroll = throttle(() => {
console.log("scrollY:", window.scrollY);
}, 200);
window.addEventListener("scroll", onScroll);How does the code work?
| Item | Meaning |
|---|---|
let timerId | The closure remembers the timer ID |
clearTimeout(timerId) | The old timer is cancelled on every new event |
setTimeout(..., delay) | The real function runs only after the delay |
lastRun | Throttle remembers the time of the last call |
now - lastRun >= interval | The function runs only if enough time has passed |
...args | The event or query reaches the real function |
In production you can also use debounce and throttle from lodash-es. They have options like leading, trailing and cancel.
Interview tip
"Write debounce and throttle yourself" is a very common question. Remember the code above, and mention that both are based on closures and timers.
6. Common mistakes
Mistake: Creating a new debounced function on every event
Why? Each time a new closure and a new timer are created, so the old timer is never cancelled and debounce does not work.
Better: Create the debounced function once and reuse it in the event listener.
Mistake: Picking the wrong technique
Why? Debounce on scroll means nothing updates while the user is scrolling, only after they stop.
Better: Use debounce when you need the final value, and throttle when you need regular updates along the way.
Mistake: Making the delay too long
Why? A 1 second debounce makes search feel slow and broken.
Better: Around 250 to 400ms for search and 100 to 200ms for scroll is usually fine.
7. What to learn before and after
| Stage | Topics |
|---|---|
| Before | Closures, Timers, DOM Events |
| Alongside | Higher-Order Functions, Event Loop |
| After | Fetch API, React useEffect |
Final quick revision
| Point | Detail |
|---|---|
| Debounce | One call after events stop |
| Throttle | At most one call in a fixed interval |
| What is inside | Closure + setTimeout or a timestamp |
| Debounce use | Search, auto save, resize |
| Throttle use | Scroll, mouse move, drag |