← All posts
Nov 18, 2026 · 5 min read

Debounce vs Throttle, Explained With Code

Two concepts I did not know about early in my web dev journey, and how to tell them apart with a few lines of JavaScript.

#javascript#performance#tutorial
--

Some of the most useful ideas in web development are the ones nobody mentions at the start. Debounce and throttle were that for me: I did not know either existed until the lack of them started causing problems. They sound alike and solve a similar problem, but they behave very differently, and mixing them up is an easy way to build something that feels broken.

The problem: events fire far more than you need

A user types the word react into a search box. Without any limiting, that is five input events and five network requests, and four of them are for text the user has already moved past. A scroll handler is worse: it can fire dozens of times a second. Debounce and throttle are two ways to say run this less often.

Debounce
Wait until the events stop for a set time, then run once. Think of it as run after the user pauses.
Throttle
Run at most once every set interval while events keep coming. Think of it as run on a steady beat.

Debounce

Every new call cancels the previous timer and starts a fresh one. The function only runs when a full wait has passed without another call.

debounce.js
function debounce(fn, wait) {
  let timer;
  const debounced = (...args) => {
    clearTimeout(timer);
    timer = setTimeout(() => fn(...args), wait);
  };
  debounced.cancel = () => clearTimeout(timer);
  return debounced;
}

input.addEventListener("input", debounce(search, 300));

Throttle

A throttled function runs right away, then ignores calls until the interval is over. It keeps updating while the events continue, just less often.

throttle.js
function throttle(fn, wait) {
  let last = 0;
  return (...args) => {
    const now = Date.now();
    if (now - last >= wait) {
      last = now;
      fn(...args);
    }
  };
}

window.addEventListener("scroll", throttle(updateProgress, 100));

Which one should I use

SituationUseWhy
Search as you typeDebounceYou only need the text once the user pauses
Autosaving a draftDebounceSave after the pause, not after every key
Recalculating layout on resizeDebounceWait until resizing has stopped
Tracking scroll positionThrottleYou need updates while scrolling, only fewer of them
Mouse move or draggingThrottleSteady updates keep the motion smooth
A button people tap repeatedlyThrottleIgnore repeats inside the window

Gotchas

  1. 01Do not recreate it on every renderIn React, a new debounced function on each render has a fresh timer, so it never debounces anything. Create it once with useMemo and keep the function it wraps stable.
  2. 02Clean up when the component goes awayA pending timer can fire after unmount. Call cancel in an effect cleanup.
  3. 03Pick a wait that fits the jobAround 200 to 300 ms feels right for search. For scroll, 100 to 200 ms is a common start. Too long and the interface feels laggy.
  4. 04For animation, use the frameIf the work is visual, such as moving something on scroll, requestAnimationFrame is often a better limiter than a fixed number of milliseconds.
React
const runSearch = useCallback((text) => {
  fetchResults(text);
}, []);

const debouncedSearch = useMemo(
  () => debounce(runSearch, 300),
  [runSearch]
);

useEffect(() => () => debouncedSearch.cancel(), [debouncedSearch]);
The takeaway

Debounce when you only care about the final value. Throttle when you care about the updates along the way, just fewer of them.

Next postHow CORS Actually Works
Book a call
patr1ck@dev — zsh
patr1ck.dev shell — type `help` to begin.
❯