Facebook Pixel

Autocomplete Debounce Implementation

Debounce is essential for autocomplete. Here is how to implement it from scratch.

Autocomplete Debounce Implementation

Debounce is essential for autocomplete. It prevents excessive API calls by waiting until the user stops typing.

What Debounce Does

Without debounce, every keystroke triggers an API call. Typing "hello" makes 5 calls (h, he, hel, hell, hello). With debounce, only 1 call is made (after the user stops typing for 300ms).

Implementation

function debounce(fn, delay) { let timer; return function (...args) { clearTimeout(timer); timer = setTimeout(() => fn.apply(this, args), delay); }; } const debouncedFetch = debounce(fetchSuggestions, 300); input.addEventListener("input", (e) => debouncedFetch(e.target.value));

How It Works

  1. Each keystroke calls the debounced function.
  2. The function clears the previous timer and sets a new one.
  3. If the user types again before the timer fires, the timer is reset.
  4. Only when the user stops typing for 300ms does the function actually run.

Why 300ms?

300ms is a good balance: fast enough to feel responsive, slow enough to skip intermediate keystrokes. Adjust based on your use case (200ms for faster, 500ms for slower).

Without Debounce (Bad)

// bad: API call on every keystroke input.addEventListener("input", (e) => { fetchSuggestions(e.target.value); });

With Debounce (Good)

// good: API call only after typing stops const debouncedFetch = debounce(fetchSuggestions, 300); input.addEventListener("input", (e) => debouncedFetch(e.target.value));

The Takeaway

Debounce: clear the previous timer on each call, set a new timer. Only the last call within the delay period executes. Use 300ms for autocomplete. This reduces API calls from N (one per keystroke) to 1 (after typing stops). Implement from scratch in interviews.

function debounce(fn, delay) { let timer; return function(...args) { clearTimeout(timer); timer = setTimeout(() => fn.apply(this, args), delay); }; }. Each call clears the previous timer and sets a new one. Only the last call within the delay period executes.

Without debounce, every keystroke triggers an API call. Typing 'hello' makes 5 calls. With debounce (300ms), only 1 call is made after the user stops typing. This reduces server load, improves performance, and provides a better user experience.

300ms is a good default. It is fast enough to feel responsive and slow enough to skip intermediate keystrokes. Use 200ms for faster response or 500ms for slower. The exact value depends on the use case and API speed.

Each call to the debounced function clears the previous setTimeout timer and sets a new one. If the function is called again before the timer fires, the timer is reset. Only when the user stops calling for the delay period does the timer fire and the function execute.

No. Debounce relies on setTimeout (or setInterval) to delay execution. Without a timer mechanism, there is no way to wait for the user to stop typing. setTimeout is the standard way to implement debounce.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.