What is the difference between debounce and throttle?

Both limit how often a function runs for events that fire very fast, like typing or scrolling.

TechniqueRunsGood for
DebounceOnce, after the events stop for some timeSearch box suggestions, resize
ThrottleAt most once in every intervalScroll and mouse move handlers
function debounce(fn, delay) {
  let timer;
  return function (...args) {
    clearTimeout(timer);
    timer = setTimeout(() => fn.apply(this, args), delay);
  };
}

input.addEventListener("input", debounce(search, 300));

The debounced function runs only 300 ms after the last key press. It uses a closure to remember the timer.


Learn more about the similar topics:
Tutorials
JavaScript Functions
JavaScript Arrow Functions
Constructor Functions in JavaScript
JavaScript this Keyword
Exercises & Assignments
No Content Found.
Interview Questions & Answers
What are arrow functions in JavaScript?
What is the this keyword in JavaScript?
What is the difference between call, apply and bind?
What is the difference between debounce and throttle?
What is the difference between function declaration and function expression?
What is a closure in JavaScript?