The original has interactive demos you can play with: https://websgeek.com/debounce-vs-throttle

Debounce and throttle are two of the most used tools for taming noisy browser events, and two of the most mixed up. This post walks through what each one does, the edge cases that cause bugs, and when neither is the right answer.

The short version

Debounce waits until events stop for a set time, then runs your function once with the last value. Throttle runs your function at most once per interval for as long as events keep coming.

So: use debounce when only the final value matters, like a search box. Use throttle when you need steady updates during the activity, like tracking a scroll position.

Both are wrappers. You hand them a function and a wait, and they return a new function that decides when the original runs. The browser still fires every event. The wrapper only filters the calls you make in response.

Debounce waits for quiet

A debounce keeps restarting a single timer. Each new event cancels the waiting timer and starts a fresh one. Your function runs only if a timer survives the full wait, which happens only after the events stop.

function debounce(fn, wait) { let timer; return (...args) => { clearTimeout(timer); timer = setTimeout(() => fn(...args), wait); }; } const search = debounce(query => fetchResults(query), 300); input.addEventListener('input', () => search(input.value)); 

Typing "strawberry" at 150 ms per letter shows the difference. With no limit that is 10 requests. A 300 ms throttle sends 6: one on the first letter, then one at the end of each of five windows. Debounce sends 1, because a 150 ms gap never reaches 300 ms. Type slower than one letter per 300 ms, though, and debounce sends every letter too. It protects you from fast typists, not slow ones.

Two caveats. Debounce doesn't fix slow responses: a request for "straw" can still arrive after the one for "strawberry", so ignore stale responses or cancel them with an AbortController. And never delay the feedback itself. The key echo should stay instant. Only the expensive consequence is limited.

In React, calling debounce() in the component body creates a new wrapper with its own timer on every render, so nothing is debounced. Create it once, pass the value in, and cancel on unmount.

Throttle keeps a beat

A throttle opens a window each time it runs. Events arriving while the window is open are held back, and only the newest is kept. When the window closes, the throttle runs once more with that newest event and opens the next window. When events stop, the windows stop too.

Edges and deadlines

A burst of events has two edges: the first event and the moment the events stop. A wrapper can call your function at either edge, both, or neither.

  • Leading gives an instant response.
  • Trailing delivers the final value. For throttle, it is what keeps the last scroll or drag position from being lost.
  • With both on, a single event causes only one call, because the trailing call needs a second event.
  • With both off, nothing ever runs.

A leading-only debounce is a neat fix for double submits:

import { debounce } from 'lodash-es'; // Run on the first click, ignore repeats until 1 s of quiet const submitOnce = debounce(sendOrder, 1000, { leading: true, trailing: false }); button.addEventListener('click', submitOnce); 

Debounce has another weakness: some activity never goes quiet, and a long paragraph typed without a pause can hold it off forever. maxWait adds a deadline. If the function hasn't run for that long, it runs anyway, then the debounce carries on. That is the right shape for autosave. Fittingly, lodash builds throttle as a debounce with both edges on and a maxWait equal to the wait.

Frames beat 16 milliseconds

For anything visual, the right update rate is the screen's refresh rate. throttle(fn, 16) is a common way to aim for about 60 updates a second, but many screens refresh faster, and a timer has no idea when the next frame is.

requestAnimationFrame runs your callback just before the browser draws the next frame. Store the newest position in the event handler and apply it once per frame:

let latestX = 0, queued = false; pad.addEventListener('pointermove', event => { latestX = event.clientX; if (queued) return; queued = true; requestAnimationFrame(() => { queued = false; handle.style.transform = `translateX(${latestX}px)`; }); }); 

On a 120 Hz screen a frame lasts 8.3 ms, so a 16 ms throttle can update at most every second frame, and roughly half of the frames with movement show an old position.

A rule for each event

Ask what the person needs: the final value, a steady stream, or a value on every frame.

  • Search, filters, validation: trailing debounce of a few hundred milliseconds, and ignore or abort stale responses.
  • Autosave: debounce with maxWait.
  • Submit buttons: leading-only debounce, plus disabling the button while the request runs.
  • Resize: CSS first, then rAF for visual changes. Debounce expensive work like redrawing a chart. Use ResizeObserver for one element.
  • Scroll: rAF for visual effects, or IntersectionObserver if you only need what is visible. Throttle with the trailing edge for analytics or loading the next page.
  • Drag and hover effects: rAF, never debounce. A debounced drag handle doesn't move until the hand stops.

To detect that scrolling has stopped, the native scrollend event is a rival to debounce. It waits until the finger lifts, while a debounce can declare the scroll over when a finger is simply held still.

Sometimes you need none of this. If the work is cheap, such as setting one transform, a wrapper only adds delay, so measure first. Whichever you pick, create it once, cancel it on teardown, and give keyboard users the same edges as pointer users.

Try it live

Play with every demo on WebsGeek: https://websgeek.com/debounce-vs-throttle

Written with AI assistance and checked against the original article by the WebsGeek team.