All topics
Performancebeginner

Debouncing and Throttling in React

Learn how to limit the rate of expensive operations triggered by fast-firing events like typing, scrolling, or resizing.

Debouncing delays executing a function until a certain amount of time has passed since the last time it was invoked, effectively collapsing a rapid burst of calls (like every keystroke while typing) into a single call once the user pauses. Throttling instead guarantees a function executes at most once per fixed time interval regardless of how many times it's triggered, useful for continuous events like scrolling or resizing where you want steady, periodic updates rather than a single final one.

Debouncing is like an elevator that waits a few extra seconds after the last person presses the button before finally closing its doors and departing, in case someone else is about to walk up; throttling is like a bus that departs on a fixed schedule no matter how many people are waiting, ensuring steady departures rather than reacting to every single new arrival.

Key Concepts

1
In React, these techniques are typically implemented via a custom hook that wraps setTimeout (for debouncing) or a timestamp/interval check (for throttling), often applied to either the value itself (a debounced search query) or the callback function that responds to an event. Debouncing a search input's API calls is the classic example: firing a network request on every keystroke would create excessive load and race conditions, while debouncing waits until typing pauses before firing just one request.
setTimeout
2
It's worth distinguishing debouncing/throttling from useTransition/useDeferredValue covered elsewhere: debouncing/throttling control *when* a function runs based on elapsed time, while React's concurrent features control *rendering priority* without necessarily delaying execution — they solve related but distinct problems and can be used together (e.g., debounce the network request, and separately use a transition to keep the results list's re-render from blocking input).
useTransitionuseDeferredValue
3
Interviewers commonly ask candidates to implement a debounced search input from scratch, checking for proper cleanup of the pending timeout in a useEffect's cleanup function to avoid firing a stale request after the component has moved on to a newer input value or unmounted.
useEffect