Skip to content
Practical SkillsChapter 45 / 47Advanced4 min read

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 controlWith debounce or throttle
An API call on every keystrokeJust one call when typing stops
Over 100 handler calls per second on scrollOne call every 200ms
Wasted load on the serverFewer requests, lower cost
The UI can lagA smooth UI

3. Where are they used?

SituationTechniqueReason
Search suggestionsDebounceSearch only after typing stops
Form auto saveDebounceSave when the user pauses
Window resizeDebounceCalculate the layout for the final size
Infinite scrollThrottleRegular checks while scrolling
Mouse move trackingThrottleUpdate the position at a fixed rate
Button double clickThrottleOnly 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.

Debounce flow
An event arrives
Cancel the old timer
Set a new timer, say 300ms
No new event arrives for 300ms
The function runs
Throttle flow
An event arrives
Has 200ms passed since the last call?
If yes, run the function and note the time
If not, ignore this event

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

js
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

js
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?

ItemMeaning
let timerIdThe 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
lastRunThrottle remembers the time of the last call
now - lastRun >= intervalThe function runs only if enough time has passed
...argsThe 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

Final quick revision

PointDetail
DebounceOne call after events stop
ThrottleAt most one call in a fixed interval
What is insideClosure + setTimeout or a timestamp
Debounce useSearch, auto save, resize
Throttle useScroll, mouse move, drag