Debounce vs Throttle Interview Questions
One of the most frequently asked frontend interview questions is:
What is the difference between Debounce and Throttle?
Many developers know the definitions:
Debounce = Wait
Throttle = LimitBut interviewers often go deeper:
- Why do we need them?
- How do they improve performance?
- When should you use one over the other?
- How are they implemented internally?
- Which one is better for search inputs, scrolling, and resizing?
Let's understand both concepts from first principles.
1. Why Do We Need Debounce and Throttle?
Modern browsers fire some events extremely frequently.
Examples:
scroll
mousemove
resize
keypress
input
touchmoveA user scrolling a page can easily trigger:
100+ events per secondWithout optimization:
window.addEventListener("scroll", fetchData);The function may execute hundreds of times.
Problems:
- Excessive API calls
- Unnecessary re-renders
- High CPU usage
- Poor user experience
Debounce and throttle help control how often functions execute.
2. What is Debounce & Implementation
Debounce delays execution until a specified period of inactivity has passed.
Example:
Type
Type
Type
Type
Stop
↓
ExecuteThe function only runs after the user stops triggering events.
Search Input Example
Without debounce:
r → API
re → API
rea → API
reac → API
react → APIFive requests.
With debounce:
r
re
rea
reac
react
↓
Wait 500ms
↓
API CallOnly one request.
Debounce Timeline
Delay:
500msEvents:
Event
Event
Event
Event
↓
Wait 500ms
↓
ExecuteOnly the final event survives.
Debounce Implementation
function debounce(fn, delay) {
let timer;
return function (...args) {
clearTimeout(timer);
timer = setTimeout(() => {
fn.apply(this, args);
}, delay);
};
}3. What is Throttle & Implementation
Throttle ensures a function executes at most once during a specified time interval.
Example:
Scroll
↓
Execute
Scroll
↓
Ignored
Scroll
↓
Ignored
1 Second Later
Scroll
↓
ExecuteThe function executes at a fixed rate.
Throttle Timeline
Interval:
1000msEvents:
Event Event Event Event Event
↓
Execute
Wait 1s
Event Event Event
↓
ExecuteUnlike debounce, events are not continuously delayed.
Throttle Implementation
function throttle(fn, delay) {
let lastExecution = 0;
return function (...args) {
const now = Date.now();
if (now - lastExecution >= delay) {
lastExecution = now;
fn.apply(this, args);
}
};
}Visual Comparison
Debounce
Event Event Event Event
↓
Wait
↓
ExecuteThrottle
Event Event Event Event Event
↓
Execute
Wait
Event Event Event
↓
Execute4. Debounce vs. Throttle: Core Differences
Debounce
Execute After Activity StopsThrottle
Execute At Fixed IntervalsThis is the most important interview answer.
Search Box Example
Consider:
<input onChange={searchUsers} />Goal:
Send API request
only after user stops typingBest choice:
DebounceBecause intermediate keystrokes are unnecessary.
Scroll Tracking Example
Consider:
window.addEventListener("scroll", updateScrollPosition);Goal:
Update position while scrollingBest choice:
ThrottleUsers expect continuous updates.
Window Resize Example
window.addEventListener("resize", updateLayout);Goal:
Run after resizing finishesBest choice:
DebounceInfinite Scroll Example
window.addEventListener("scroll", loadMoreData);Goal:
Check position continuouslyBest choice:
Throttle5. Real Frontend Use Cases & Timeline Comparison
Debounce
Commonly used for:
- Search autocomplete
- Form validation
- Auto-save drafts
- Window resize
- API requests
Example:
debounce(searchUsers, 500);Throttle
Commonly used for:
- Scroll listeners
- Mouse movement tracking
- Drag events
- Infinite scrolling
- Analytics tracking
Example:
throttle(trackMouse, 100);Interview Question
Which is better?
Debounce or Throttle?Answer:
Neither.They solve different problems.
Choose based on desired behavior.
6. React Integration Patterns
const debouncedSearch = useMemo(
() =>
debounce((value) => {
fetchUsers(value);
}, 500),
[],
);Usage:
<input onChange={(e) => debouncedSearch(e.target.value)} />Reduces API requests.
React Example: Throttle
useEffect(() => {
const handler = throttle(() => {
console.log(window.scrollY);
}, 200);
window.addEventListener("scroll", handler);
return () => window.removeEventListener("scroll", handler);
}, []);Improves scroll performance.
Debounce with Immediate Flag
Some implementations support:
debounce(fn, 500, true);Behavior:
First Event
↓
Execute Immediately
Subsequent Events
↓
Ignored
Wait 500ms
↓
Ready AgainThis is called:
Leading Edge Debounce7. API Call Sizing: Throttle vs. Debounce
Suppose a user types:
r
re
rea
reac
reactDebounce
1 API CallThrottle
May Trigger Multiple CallsExample:
r
↓
API
rea
↓
API
react
↓
APIFor search APIs:
Debounce is usually preferred8. Common Interview Q&A
Which One Improves Performance More?
Both improve performance.
The correct choice depends on behavior requirements.
Can Debounce Completely Skip Intermediate Events?
Yes.
That is its purpose.
Only the final event executes.
Can Throttle Skip Events?
Yes.
Events occurring within the throttle interval are ignored.
Which One Is Better for Search Inputs?
DebounceBecause users care about the final input.
Which One Is Better for Scroll Events?
ThrottleBecause updates must happen continuously.
What JavaScript Concept Powers Debounce?
Closures.
Example:
let timer;remains available through a closure.
What JavaScript Concept Powers Throttle?
Closures.
Example:
let lastExecution;persists between function calls.
Senior-Level Interview Answer
Debounce and throttle are performance optimization techniques used to control the frequency of function execution. Debounce delays execution until a period of inactivity has passed, making it ideal for search inputs, form validation, and auto-save features. Throttle limits execution to at most once per specified interval, making it ideal for scroll, resize, and mouse movement events. Internally, both techniques rely on closures to maintain state between invocations. The choice depends on whether the application needs the final event only (debounce) or regular updates during continuous activity (throttle).
Common Interview Mistakes
❌ "Debounce and throttle do the same thing."
Correct:
Debounce waits for inactivity.
Throttle limits execution frequency.❌ "Throttle executes only once."
Correct:
Throttle executes repeatedly,
but at a controlled rate.❌ "Debounce is always better."
Correct:
Each solves a different problem.❌ "Debounce and throttle are built into JavaScript."
Correct:
They are commonly implemented
using closures and timers.Debounce vs Throttle Summary
| Feature | Debounce | Throttle |
|---|---|---|
| Goal | Wait for inactivity | Limit execution frequency |
| Intermediate Events | Ignored | Some may execute |
| Search Inputs | ✅ Best Choice | ❌ Usually Not |
| Scroll Events | ❌ Usually Not | ✅ Best Choice |
| Resize Events | ✅ Common | Sometimes |
| API Calls | ✅ Common | Less Common |
| Implementation | Timer Reset | Timestamp / Timer |
Key Takeaways
- Debouncing: Postpones execution until a specified delay has elapsed since the last event tick (e.g. search suggestions).
- Throttling: Enforces a maximum execution frequency, running the callback at most once per duration (e.g. scroll tracking).
- Interactive Control: Debounce groups bursts into a single call; throttle spaces out continuous execution.
- React Patterns: Use custom hooks or refs inside React to preserve debounced/throttled instances across renders.