CodeZettaInterview Hub

JavaScript · Performance · Fresh / Intern · Comparison

What is the difference between debounce and throttle?

Short Interview Answer

Debounce waits until events pause before running; throttle runs at most once per interval while events continue.

Detailed Explanation

Debounce: useful for search-as-you-type — fire after the user stops typing. Throttle: useful for scroll/resize handlers — ensure periodic updates without flooding. Both reduce main-thread work. Implement carefully with leading/trailing options. Prefer framework utilities but understand the semantics in interviews. Combine with `requestAnimationFrame` for visual updates.

Example

function debounce(fn, ms) {
  let t;
  return (...args) => {
    clearTimeout(t);
    t = setTimeout(() => fn(...args), ms);
  };
}

Interview Tip

Give one UX example for each.

Common Mistake

Using the terms interchangeably.

Did you know this answer?