ARTICLE
Featured

Spilling the Tea on Debounce & Throttle in JavaScript

Ever been on a website that just feels… slow? Like, you type in a search bar and the whole page stutters? Or you scroll, and it's super laggy? Lowkey, that's a major vibe kill. A lot of the time, the culprit is your code firing off a million events at once.

Nailul Ngafwa
September 22, 2025
4 min read
Java Script
Spilling the Tea on Debounce & Throttle in JavaScript

Debounce: The "Chill, Wait a Sec" Vibe

Imagine you're texting your friend. You don't send a new message for every single letter you type, right? You type a whole thought, pause for a sec, and then hit send. That's literally what debouncing is.

It's a function that waits for a certain amount of time after an event has stopped firing before it actually runs.

The Use Case: This is perfect for stuff like search bars. You don't want to hit an API for every single letter a user types (s, se, sea, sear, searc, search... nah). Instead, you wait until they've stopped typing for, say, 300 milliseconds, and then you fetch the results. It saves a ton of unnecessary network requests and makes your app feel way snappier.

The Code:

Here’s a simple debounce function. Peep the comments to see what's up.

// Our debounce function
// It takes two things: the function you want to run (func) and how long to wait (delay)
const debounce = (func, delay) => {
  let timeoutId; // This will keep track of our timer

  // This is the function that will actually be called when the event happens
  return (...args) => {
    // Every time the event fires, we clear the previous timer
    clearTimeout(timeoutId);

    // Then we set a *new* timer
    timeoutId = setTimeout(() => {
      // Once the timer is up, we finally run the original function
      func(...args);
    }, delay);
  };
};

// --- How to use it ---
const searchInput = document.getElementById('search-bar');

const handleSearch = (event) => {
  // This is where you'd do your API call or filtering logic
  console.log(`Searching for: ${event.target.value}`);
};

// We wrap our handleSearch function in the debounce function
// It will only run after the user stops typing for 500ms
searchInput.addEventListener('input', debounce(handleSearch, 500));

Throttle: The "Okay, One at a Time" Method

Now for throttling. If debounce is about waiting for a pause, throttle is about setting a hard limit. It's like, "Hey, you can only run this function once every X milliseconds, idc how many times you try."

The Use Case: Throttling is clutch for events that fire continuously, like scrolling, resizing a window, or dragging an element. Imagine you have an animation that triggers on scroll. Without throttling, you could be running that animation function hundreds of times per second, which will absolutely cook the user's CPU. 

By throttling it to run, say, once every 100 milliseconds, the animation still looks smooth, but you're not overwhelming the browser. It's all about working smarter, not harder.

The Code:

Here’s what a basic throttle function looks like.

// Our throttle function
// It also takes the function to run (func) and the time limit (limit)
const throttle = (func, limit) => {
  let inThrottle; // A flag to check if we're currently "cooling down"

  return (...args) => {
    // If we're not in the cooldown period...
    if (!inThrottle) {
      // ...run the function immediately
      func(...args);
      // Then, set the flag to true to start the cooldown
      inThrottle = true;
      // And set a timer to reset the flag after the limit has passed
      setTimeout(() => (inThrottle = false), limit);
    }
  };
};

// --- How to use it ---
const handleScroll = () => {
  // Logic that runs on scroll, like showing a "back to top" button
  console.log('User is scrolling!');
};

// We wrap our scroll handler in the throttle function
// It will only log to the console at most once every 1000ms (1 second)
window.addEventListener('scroll', throttle(handleScroll, 1000));

So, When to Use What?

It can be a little confusing, so here's the main difference:

  • Use Debounce: When you only care about the final state. Think search input, where you only want the final typed value.
  • Use Throttle: When you want to handle events at a regular, controlled rate as they happen. Think scrolling, window resizing, or shooting a gun in a video game (you can't have infinite bullets per second!).

For real, mastering these two concepts will level up your front-end game. Your users will thank you for the buttery-smooth experience, and your code will be way more efficient. Go build something awesome!

Published on September 22, 2025