MohitDecodes
Back to blogsJavaScript

Debounce vs Throttle in JavaScript (With Real UI Examples)

October 30, 2026 · 6 min read

Typing into a search box or scrolling a page can fire dozens of events per second. Without rate control, your app either floods the backend with API calls or drops frames trying to recalculate layout.

Debounce: wait until the user stops

Debouncing delays running a function until a quiet period has passed since the *last* event. If the user keeps typing every 100ms, the timer resets each time and the search API only fires once they pause.

function debounce(fn, delay = 300) {
  let timer;
  return (...args) => {
    clearTimeout(timer);
    timer = setTimeout(() => fn(...args), delay);
  };
}

Throttle: run at most once every N milliseconds

Throttling guarantees the function runs on a steady rhythm while events keep firing — ideal for scroll progress bars, window resize handlers, or drag tracking where waiting until the user completely stops would feel laggy.

  • Use debounce for search autocomplete, form validation, and auto-save.
  • Use throttle for infinite scroll checks, sticky navbar animations, and analytics scroll tracking.

Further reading