JS Guide
HomeQuestionsTopicsCompaniesResources
BookmarksSearch

Built for developers preparing for JavaScript, React & TypeScript interviews.

ResourcesQuestionsSupport
HomeQuestionsSearchProgress
HomeQuestionsjavascript
PrevNext

Learn the concept

Debounce & Throttle

javascript
mid
debounce-throttle

How do debounce and throttle work, and when would you use each?

debounce
throttle
performance
patterns
events
Quick Answer

Debounce delays execution until a pause in activity (e.g., wait 300ms after the user stops typing). Throttle limits execution to at most once per time interval (e.g., at most once every 200ms during scrolling). Both are used to control how often expensive functions run.

Detailed Explanation

Both debounce and throttle control function execution frequency, but they work differently:

Debounce:

  • Waits for a pause in events before executing
  • Resets the timer on each new event
  • Function only runs after events stop for the specified delay
  • Use for: search input, form validation, window resize end, auto-save

Throttle:

  • Executes at regular intervals during continuous events
  • Guarantees the function runs at most once per interval
  • First call executes immediately (in leading-edge mode)
  • Use for: scroll handlers, mouse move, rate-limited API calls, game loops

Key difference:

  • Debounce: "Wait until they stop, then execute"
  • Throttle: "Execute at most once every N ms"

Note: The basic throttle implementation below is leading-edge only (executes on the first call). Production throttles (e.g., lodash _.throttle) also support a trailing-edge call so the last event is never silently dropped.

Code Examples

Debounce implementationJavaScript
function debounce(fn, delay) {
  let timeoutId;
  return function (...args) {
    clearTimeout(timeoutId);
    timeoutId = setTimeout(() => fn.apply(this, args), delay);
  };
}

// Usage: search input
const searchInput = document.querySelector('#search');
const handleSearch = debounce((query) => {
  console.log('Searching for:', query);
  // fetch(`/api/search?q=${query}`)...
}, 300);

searchInput.addEventListener('input', (e) => {
  handleSearch(e.target.value);
});
// Only fires 300ms after the user stops typing

Real-World Applications

Use Cases

Search Autocomplete

Debouncing search input to reduce API calls while the user types, only firing after they pause

Scroll-based UI

Throttling scroll handlers for sticky headers, parallax effects, and infinite scroll to limit DOM calculations

Window Resize

Debouncing resize handlers to recalculate layouts only after the user finishes resizing the window

Mini Projects

Autocomplete Search

beginner

Build a search input that debounces API calls and displays autocomplete suggestions with loading state

Infinite Scroll Feed

intermediate

Build a content feed with throttled scroll detection for lazy loading new items as the user scrolls

Industry Examples

Lodash

_.debounce and _.throttle with leading/trailing edge options and cancel/flush methods for fine-grained control

React

useDeferredValue achieves a similar goal to debouncing (keeping UI responsive during rapid updates) by deferring lower-priority re-renders

Resources

MDN - Debounce and Throttle

docs

JavaScript.info - Debounce and Throttle

article

Related Questions

What is event delegation in JavaScript and why is it useful?

junior
events

What is the difference between debouncing and throttling?

mid
optimization
Previous
What are Promise.all, Promise.race, Promise.allSettled, and Promise.any, and when do you use each?
Next
What is currying and partial application in JavaScript?
PrevNext