Debouncing vs Throttling in JavaScript
Two event optimization techniques. Here is the difference and when to use each (Walmart question).
Debouncing vs Throttling in JavaScript
Debouncing and throttling are both event optimization techniques, but they work differently.
Debounce: Delay Until Activity Stops
function debounce(fn, delay) { let timer; return function (...args) { clearTimeout(timer); timer = setTimeout(() => fn.apply(this, args), delay); }; }
Each call resets the timer. Only the last call executes (after the delay).
Use for: search input, window resize, auto-save.
Throttle: Limit to Once Per Interval
function throttle(fn, delay) { let last = 0; return function (...args) { const now = Date.now(); if (now - last >= delay) { last = now; fn.apply(this, args); } }; }
The function executes at most once per delay ms. Calls in between are ignored.
Use for: scroll, mousemove, resize (continuous events).
Key Difference
- Debounce: waits for activity to stop, then runs once.
- Throttle: runs at a fixed rate, regardless of activity.
Example
Typing "hello" with 300ms:
- Debounce: runs once, 300ms after the last keystroke.
- Throttle: runs at most once every 300ms (may run 1-2 times during typing).
The Takeaway
Debounce: delay until activity stops (last call wins). Use for search, resize, auto-save. Throttle: limit to once per interval (first call wins, then rate-limited). Use for scroll, mousemove, resize. Asked by Walmart.
Debounce delays execution until activity stops (only the last call runs). Throttle limits to once per interval (calls at a fixed rate, ignoring calls in between). Debounce = wait then run; throttle = run at most every N ms.
Use debounce for search input, auto-save, and window resize (run after activity stops). Use throttle for scroll, mousemove, and continuous events (run at a controlled rate, not after stopping).
function debounce(fn, delay) { let timer; return function(...args) { clearTimeout(timer); timer = setTimeout(() => fn.apply(this, args), delay); }; }. Each call clears and resets the timer. Only the last call executes.
function throttle(fn, delay) { let last = 0; return function(...args) { const now = Date.now(); if (now - last >= delay) { last = now; fn.apply(this, args); } }; }. Execute only if enough time has passed since the last execution.
Walmart asks candidates to explain the difference between debouncing and throttling, implement both from scratch, and explain when to use each. They may also ask you to implement a specific use case (e.g., throttle a scroll handler).
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.
Master React
Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course.

