Debounce vs throttle in JavaScript
How the interviewer asks it
“Write debounce(fn, wait) and throttle(fn, wait) in JavaScript without a library. Explain the difference with one real use for each, and what happens to this and the arguments of the original call.”
Typed drills are free and unlimited: the AI scores your answer against the outline below.
What the interviewer is looking for
- A clear difference: debounce waits for a pause, throttle runs at most once per interval.
- A real use for each, like search input for debounce and scroll or resize for throttle.
- Correct closures over the timer, with this and the latest arguments passed through.
- Awareness of leading and trailing calls and of cancelling a pending call.
How to structure your answer
- 01
Define
Debounce delays the call until no new call has come for wait ms; throttle lets one call through per wait ms.
- 02
Debounce
Keep a timer in a closure; on each call clear it and set a new one that calls fn.apply(this, args).
- 03
Throttle
Remember the time of the last run; run now if wait has passed, otherwise schedule one trailing call with the latest arguments.
- 04
Edge cases
Preserve this with a regular function, not an arrow; offer cancel() and a leading option; clean up timers when a component unmounts.
Best answers from candidates
No candidate answers yet. Type yours, and if it scores 6 or more you can show it here without your name.
Answers are shared by candidates, without names, after a privacy check. Add yours
Common mistakes
- Mixing the two up, or saying they are the same thing.
- Losing this or the arguments by calling fn() directly.
- Creating a new debounced function on every render in React, so it never debounces.
Follow-up questions to expect
- Add a leading option to debounce and a cancel() method. What changes in your code?
- How would you use a debounced handler correctly inside a React component?
Now answer it out loud
Max will ask you this exact question, listen to your answer, push on the weak spots with follow-ups like the ones above, and score you when you finish.
Practice with MaxFree: a 5-minute voice round and unlimited typed drills, with scored feedback. No card, and your camera is never recorded.