JavaScript Throttling vs Debouncing: Control Event Frequency
Learn how throttling limits repeated calls, compare it with debouncing, and implement a throttle for scroll and resize events.

What is Throttling, and How is it different from Debouncing?
Some browser events can fire many times in a short period. A scroll handler, for example, may run repeatedly while someone scrolls. If the handler does expensive work every time, the page can feel sluggish.
Throttling limits how often a function runs. Even if its trigger continues firing, the function runs at most once during a chosen time interval. Debouncing waits until triggers have stopped for the full delay, then runs once. A short memory aid is: debounce waits for quiet; throttle runs at a controlled pace.
| Pattern | When it runs | Good fit |
|---|---|---|
| Debounce | After triggers pause for the delay | Search input, save-after-edit |
| Throttle | At most once per interval while triggers continue | Scroll, resize, drag updates |
Make a simple throttle
This version runs on the first event, then allows another call after the interval has elapsed:
function throttle(callback, delay) {
let lastRun = 0;
return function (...args) {
const now = Date.now();
if (now - lastRun < delay) {
return;
}
lastRun = now;
callback.apply(this, args);
};
}
lastRun lives in the closure, so it remembers when the callback last ran across calls. If another event happens before delay milliseconds have passed, the wrapper returns without calling the callback. When enough time has passed, it updates the timestamp and runs the callback with the latest event arguments.
This is a leading-only throttle: events inside the interval are dropped. Throttle implementations can also schedule a trailing call at the end of the interval with the latest arguments. Choose the behavior your interface needs.
Use throttling with scroll
Assume this page has a function that updates a reading-progress display. Throttle it so it does not run for every scroll event:
function updateReadingProgress() {
const scrollableHeight =
document.documentElement.scrollHeight - window.innerHeight;
const progress = scrollableHeight > 0 ? window.scrollY / scrollableHeight : 0;
console.log(`${Math.round(progress * 100)}% read`);
}
const throttledProgress = throttle(updateReadingProgress, 100);
document.addEventListener("scroll", throttledProgress);
The browser can fire scroll events at a high rate. The wrapper caps how frequently updateReadingProgress() runs, reducing repeated work. Pick an interval that keeps the UI responsive without doing unnecessary calculations. For scroll-specific performance guidance, see MDN's scroll event documentation.
Timeline example
Suppose input events happen at 0, 80, 160, 240, and 500 milliseconds, with a 200-millisecond delay:
- Debounce: resets its timer after each event. It runs after the last event, at about 700 ms.
- Throttle: runs at 0 ms, ignores events during the next 200 ms, then runs on the next event after that interval (around 240 ms), and again when an eligible event arrives after 500 ms.
Exact timing depends on event timing and how the browser schedules timers. A setTimeout() delay is a minimum wait, not a promise that code will run at that exact millisecond. MDN: setTimeout()
Which one should you use?
Use debounce when only the final value matters after activity stops, such as starting a search after typing. Use throttle when you need periodic updates during ongoing activity, such as tracking scroll progress or responding to a drag.
Throttle is also not a substitute for optimizing expensive work. If a handler still takes too long, reduce the work it performs or use a more suitable browser API. For scroll events, MDN recommends measuring a timeout interval rather than assuming requestAnimationFrame() will throttle the event.
Try it yourself
Attach a throttled function to a resize event that logs the current window width. Set the interval to 250 milliseconds and resize the browser continuously. Compare the output with an unthrottled resize handler.
Recap
- Throttling limits a function to at most one run per chosen interval.
- Debouncing waits until repeated triggers stop for the delay, then runs once.
- Throttling is useful for ongoing scroll, resize, or drag activity.
- Leading-only and leading-plus-trailing throttle implementations behave differently; choose intentionally.
What's next?
Next, we’ll learn event delegation and why it is useful for dynamic content.
Written by
Dev Likhon
Full Stack Developer & Tech Educator
6+ years of professional development experience.
About Me
Comments
Share what you learned or ask a question.
Loading comments...