Debounce
- Pronunciation
- dee-BOWNSS
In short
Debouncing delays a function until a burst of events has paused for a set time, so a search box sends one request after typing stops, not one per keystroke.
What is debouncing?
Some browser events fire very often: every keystroke, every scroll step, every pixel of a window resize. Running expensive work on each one, such as an API request or a layout calculation, wastes resources and can make the page stutter. A debounced function restarts a timer on every event and only runs once the events have been quiet for, say, 300 milliseconds.
Its sibling is throttling, which guarantees the function runs at most once per interval, however many events arrive. Throttling suits continuous actions where steady updates matter, such as updating a position while scrolling or dragging; debouncing suits actions where only the final value matters, such as search-as-you-type, autosave or validating a field.
Debounce can run on the trailing edge, after the pause, which is the usual choice, or on the leading edge, immediately on the first event and then ignoring the rest of the burst, which suits buttons that shouldn't be double-clicked. Utility libraries such as Lodash provide both helpers, and they are only a few lines to write by hand.
A common misconception is that debouncing makes an app faster. It reduces how often work is done, but it also adds a delay before the result appears. Pick the wait so it feels responsive, and remember that late responses from earlier requests can still arrive, so search code should also cancel or ignore stale requests.
Key takeaways
- Debounce waits until events stop for a set time, then runs once.
- Throttle runs at most once per interval during continuous events.
- Use debounce for search-as-you-type, autosave and validation.
- Use throttle for scrolling, resizing and dragging.
- It adds a short delay, and stale requests still need handling.
Example
function debounce(fn, wait) {
let timer;
return (...args) => {
clearTimeout(timer); // every new event restarts the wait
timer = setTimeout(() => fn(...args), wait);
};
}
const search = debounce(async (query) => {
const results = await fetch(`/api/search?q=${encodeURIComponent(query)}`).then((r) => r.json());
render(results);
}, 300);
document.querySelector("#q").addEventListener("input", (e) => search(e.target.value));
// Typing "react" sends one request, 300 ms after the last key.Readers ask
What is the difference between debounce and throttle?
Debounce waits for a pause and runs once at the end of a burst of events. Throttle runs regularly, at most once per interval, while the events keep coming.
What delay should I use for debouncing?
For typing, around 200 to 400 milliseconds feels responsive without sending a request per key. For expensive work such as autosaving, a second or more is common.
Where does the name debounce come from?
From electronics: a mechanical switch bounces and sends several signals when pressed once, and debouncing circuits treat that burst as a single press.
Often compared
See also
- JavaScriptWeb Development, p. 24JavaScript is the programming language that runs in web browsers to make pages interactive, and it also runs on servers through runtimes like Node.js.
- DOMWeb Development, p. 13The DOM is the browser's in-memory tree of objects representing a web page, which JavaScript can read and change to update what the user sees.
- Rate LimitingBackend & APIs, p. 37Rate limiting is a technique that caps how many requests a client can make to a server or API within a time window, protecting it from abuse and overload.
- AJAXWeb Development, p. 1AJAX (Asynchronous JavaScript and XML) is a technique for loading server data in the background with JavaScript and updating part of a page without a reload.
- CallbackProgramming Fundamentals, p. 7A callback is a function passed as an argument to another function, which then calls it later, for example when a task finishes or an event happens.
- Core Web VitalsWeb Development, p. 7Core Web Vitals are three metrics that measure a page's real-user experience: loading speed (LCP), responsiveness (INP), and visual stability (CLS).
- ThrottlingWeb Development, p. 47Throttling makes a function run at most once per interval, however often it is called, so scroll and resize handlers update steadily without running every time.
Sources
Spotted a mistake or something missing on this page?Suggest an edit