Skip to the solution
HardFrontend7 min read

What is debouncing and throttling?

113 views

The problem

Question presented to candidate: "Can you explain debouncing and throttling, what problem they both solve, and how you would decide which one to use for a given UI event?"

What a strong answer should cover:

  • Both techniques exist to stop a high-frequency event (scroll, resize, keystroke, mousemove) from running an expensive handler far more often than necessary.
  • Debounce: collapses a burst of calls into a single call that runs only after the activity has genuinely paused for a set delay. Every new call resets the wait.
  • Throttle: guarantees the handler runs at most once per fixed interval, no matter how many events fire during that interval. It does not wait for silence.
  • The concrete decision rule: use debounce when you only care about the final state after activity stops (search-as-you-type, autosave, form validation). Use throttle when you need steady, periodic feedback throughout continuous activity (scroll position tracking, drag handlers, mousemove-driven UI).
  • Both rely on the same underlying mechanism: a closure holding onto timer state (a timeout id, or a cooldown flag/last-run timestamp) across calls.

Clarifying questions expected:

  • "Do you want the conceptual distinction, or should I also implement one of them?" (this question is definitional; a good candidate confirms scope before writing code)

Code / implementation expected: Optional β€” a short side-by-side runnable snippet proving the behavioral difference is enough; full implementations belong to the dedicated implement-debounce / implement-throttle follow-up questions.

  • performance
  • optimization
  • debouncing
  • throttling

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

7 min read Β· 7 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptSame 15-call burst through both debounce and throttle (run directly)

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 164 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextHard

What is a Proxy in JavaScript?

Continue