---
title: "Debounce vs throttle in JavaScript: what is the difference?"
description: "Debounce waits until events stop before running a function; throttle runs it at most once per interval. Learn when to use each, with a small implementation of both."
url: https://www.danny.engineering/answer/javascript/debounce-vs-throttle-in-javascript-what-is-the-difference
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:24:00.000Z
tags: ["Javascript"]
---

# Debounce vs throttle in JavaScript: what is the difference?

## The short answer


Both limit how often a function runs when an event fires many times. Debounce waits until the events have stopped for a set time, then runs once. Throttle runs at most once per interval while the events keep coming. Use debounce for search boxes and resize handlers that only need the final value; use throttle for scroll or pointer tracking that needs steady updates.


```javascript
searchInput.addEventListener('input', debounce(runSearch, 300));
window.addEventListener('scroll', throttle(updateProgressBar, 100));
```


## Why it happens


Events such as `input`, `scroll`, `resize` and `pointermove` can fire dozens of times a second. If each one triggers a network request or a layout calculation, the page slows down or the server gets flooded.


The two techniques solve slightly different problems:

- Debounce cares about the end of a burst. Typing "react" fires five input events, but you only want to search once the user pauses.
- Throttle cares about a steady rate. While the user scrolls for three seconds, you want regular updates, not one at the end.

## How to fix it


### A small debounce


Each call clears the previous timer and starts a new one, so the function only runs once the calls stop for `wait` milliseconds.


```javascript
function debounce(fn, wait) {
  let timer;
  return function (...args) {
    clearTimeout(timer);
    timer = setTimeout(() => fn.apply(this, args), wait);
  };
}

const onInput = debounce((event) => {
  fetchResults(event.target.value);
}, 300);

searchInput.addEventListener('input', onInput);
```


### A small throttle


This version runs straight away on the first call, ignores calls during the interval, and runs once more at the end with the latest arguments so the final position is not lost.


```javascript
function throttle(fn, interval) {
  let last = 0;
  let timer;
  let lastArgs;

  return function (...args) {
    const now = Date.now();
    const remaining = interval - (now - last);
    lastArgs = args;

    if (remaining <= 0) {
      clearTimeout(timer);
      timer = undefined;
      last = now;
      fn.apply(this, args);
    } else if (!timer) {
      timer = setTimeout(() => {
        last = Date.now();
        timer = undefined;
        fn.apply(this, lastArgs);
      }, remaining);
    }
  };
}

window.addEventListener('scroll', throttle(() => {
  progress.style.width = `${getScrollPercent()}%`;
}, 100));
```


### Throttle visual updates with requestAnimationFrame


If the work only updates the screen, tie it to the browser's paint cycle instead of a fixed interval. It runs at most once per frame.


```javascript
let scheduled = false;

window.addEventListener('scroll', () => {
  if (scheduled) return;
  scheduled = true;
  requestAnimationFrame(() => {
    scheduled = false;
    updateStickyHeader();
  });
});
```


### Choosing between them

- Search as you type, autosave, form validation: debounce.
- Window resize where you only need the final size: debounce.
- Scroll position, infinite scroll checks, drag handling: throttle.
- Animations or anything purely visual: `requestAnimationFrame`.

## Common pitfalls

- Creating a new debounced function on every event or render. Each one has its own timer, so nothing is debounced. Create it once and reuse it.
- Losing `this` by using an arrow function for the returned wrapper when you need the element. The examples use a regular function for that reason.
- Debouncing scroll handlers that should update continuously. The UI only changes after scrolling stops.
- Forgetting to cancel pending timers when a component unmounts. Add a `cancel` method that calls `clearTimeout` if you need one.
- Using throttle to save network requests while typing. The user still sends partial queries; debounce is usually better there.

## Related reading

- [MDN: Debounce](https://developer.mozilla.org/en-US/docs/Glossary/Debounce)
- [MDN: Throttle](https://developer.mozilla.org/en-US/docs/Glossary/Throttle)
- [MDN: requestAnimationFrame()](https://developer.mozilla.org/en-US/docs/Web/API/Window/requestAnimationFrame)
