Basics

What is debounce — and why your search box sends six requests instead of one

Illustration: six keypresses merging into a single request

You type "london" into a search box. Six letters. How many requests go to the server?

Six. One per letter — because the input event fires on every keystroke. And five of them are already useless: the user typed to the end anyway.

While it's just a database, that's only wasted load. But if there's an AI suggestion behind that box, you just paid for six model calls instead of one. For one word.

The cure is one technique, about five lines long.

What debounce is

Debounce is a rule: do nothing until the events stop coming.

Each new keystroke doesn't fire a request — it cancels the previous pending one and schedules a new one. The request only goes out once the person has been quiet for 300 milliseconds.

The analogy is an elevator door. It doesn't close on a timer started by the first passenger: every new arrival resets the count. You move when the flow dries up.

The word comes from electronics: a mechanical button "bounces" when pressed — the contact opens and closes several times within milliseconds. The circuit that waits for the bouncing to settle is called a debouncer. A keyboard is the same: the human types fast, but has one single intention.

What it looks like in code

The whole mechanism is a timer variable and clearTimeout:

let timer;

input.addEventListener('input', (e) => {
  clearTimeout(timer);                     // cancel the previous plan
  const q = e.target.value;
  timer = setTimeout(() => search(q), 300); // schedule a new one
});

Six letters — six clearTimeout calls and one search. That's it.

The one thing that's easy to get wrong: capture the value into a variable before setTimeout, as above. If you read e.target.value again inside the timer, you may catch a different state of the field.

debounce and throttle are not the same thing

These get mixed up most often, and they behave in opposite ways.

debounce — "wait for silence". While events keep coming it does nothing. It fires once, at the end. If someone types non-stop for a minute, there'll be no request for a minute.

throttle — "no more often than". It lets one event through, mutes everything for 300 ms, then lets another through. It works regularly during the flow.

Hence the rule for choosing:

  • Only the result matters — debounce. Search-as-you-type, draft autosave, field validation.
  • The process matters — throttle. Scroll position, dragging an element, a progress counter.

Put throttle on search and you get a request every 300 ms while typing — half the problem back. Put debounce on scroll and the sticky header only appears once the user stops. Both read as bugs.

The real trap: answers arrive out of order

Here's the reason to use debounce that almost nobody writes about. It isn't only about saving money — it's about a correct result.

Imagine the requests did go out: first "lon", then "london". The answer for "london" comes back in 80 ms; the answer for "lon" takes 400. What does the user see?

Results for "lon". Because they arrived later and overwrote the correct ones.

That's a classic race, and it's unreproducible on your fast connection — while your user on the subway hits it regularly. Debounce removes nearly all of these simply because only one request survives. To close the rest, cancel the stale request properly:

let controller;

async function search(q) {
  controller?.abort();                 // kill the previous one
  controller = new AbortController();
  const res = await fetch(`/api/search?q=${q}`, { signal: controller.signal });
  render(await res.json());
}

AbortController is the standard way to interrupt a fetch. Without it the old answer still lands and still ruins things. If async is still fuzzy, start with the piece on async.

How long to wait — and what you gain

About 300 ms: it isn't magic, it's a compromise. The average gap between keystrokes in ordinary typing is around 150–200 ms, so 300 reliably survives the pause between letters without feeling laggy.

Practical starting points:

  • 150–250 ms — live search where responsiveness matters.
  • 300–500 ms — when an AI call or a heavy computation sits behind the field.
  • 1000 ms and up — autosave. Nothing to rush here.

The win is easy to count. A search box, 20 characters typed, 100 users a day: without debounce that's 2000 requests, with it about 100. If every request hits a model, a twentyfold difference lands straight on your bill — right next to the other tricks in how to cut your AI costs.

And no, client-side debounce does not replace a server-side rate limit: the first saves requests from a polite user, the second protects you from an impolite one. How search-as-you-type is built in the first place is in how to add search to your app.

Is debounce only for search?

No. Any event that arrives in bursts: window resize, scroll, autosave while typing, recalculating a cart when quantity changes, checking whether a username is taken. The signal is always the same — many events, but only the last result matters.

Can I use a library function instead of my own?

Sure, debounce ships in lodash and in a dozen tiny packages. But your own version is the five lines above, and writing it once pays off: then you understand why in React it has to be wrapped in useMemo or useRef, or every render creates a fresh timer and debounce quietly stops working.

Details like this are what separate an app that "kind of works" from one that works. In KODiQ we walk through them in plain language, step by step, on real examples.

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →