HomeAbout Me

Debounce in JavaScript

By Daniel Nguyen
Published in Javascript
July 15, 2025
1 min read

🚀 Understanding Debounce in JavaScript: A Simple Guide with Examples

When working with JavaScript—especially in the browser—it’s common to run into performance issues when certain functions are called too frequently. Think of scrolling, resizing a window, or typing into an input field. If you’re not careful, this can lead to laggy UIs and a bad user experience.

That’s where debounce comes to the rescue.


🤔 What Is Debounce?

Debounce is a programming technique used to limit how often a function is executed. It ensures that the function only runs after a specified amount of time has passed since the last time it was invoked.

Real-world Analogy:

Imagine you’re typing a search query. You don’t want to send a network request to the server every time you press a key. Instead, you want to wait until the user pauses typing for, say, 500ms—then send the request. That’s debounce.


🧠 Why Use Debounce?

Without debounce, functions tied to events like:

  • scroll
  • resize
  • keyup
  • mousemove

…can fire dozens or hundreds of times per second, causing:

  • Performance bottlenecks
  • UI stutter
  • Excessive API calls

🧪 A Basic Debounce Function

Let’s build a simple debounce utility:

function debounce(fn, delay) {
let timerId;
return function (...args) {
clearTimeout(timerId); // cancel previous timer
timerId = setTimeout(() => {
fn.apply(this, args);
}, delay);
};
}

Parameters:

  • fn: the function to debounce
  • delay: the amount of time (in milliseconds) to wait after the last call

Let’s say you’re building a search bar:

<input type="text" id="search" placeholder="Search..." />

And here’s how to debounce the input handler:

const input = document.getElementById("search");
function handleSearch(event) {
console.log("Searching for:", event.target.value);
}
const debouncedSearch = debounce(handleSearch, 500);
input.addEventListener("input", debouncedSearch);

Now, handleSearch will only run after the user stops typing for 500ms.


⚙️ When Should You Use Debounce?

Use debounce when:

  • You want to delay a function call until the user stops performing a high-frequency action.
  • You’re handling user input, like in autocomplete or search bars.
  • You’re dealing with expensive operations like API calls, layout recalculations, or DOM updates.

❓ Debounce vs Throttle: What’s the Difference?

TechniquePurposeExecution Frequency
DebounceWaits until a pause in activityOnce after the last event
ThrottleLimits how often a function runsAt regular intervals

Use debounce when you care about the final result, and throttle when you want to sample regularly (e.g., on scroll).


🧰 Debounce in Utility Libraries

If you don’t want to write your own, libraries like Lodash provide a reliable debounce method:

import debounce from 'lodash.debounce';
const optimizedFn = debounce(() => {
console.log("Optimized!");
}, 300);

✅ Conclusion

Debounce is an essential tool for crafting performant, responsive front-end experiences. By waiting until a burst of events stops before running a function, you can:

  • Prevent performance issues
  • Reduce unnecessary operations
  • Improve UX

Next time you’re working with a scroll event or search box, reach for debounce and keep your app snappy! ⚡



Tags

#Javascript

Share

Previous Article
React Performance: Expensive Calculations

Table Of Contents

1
🤔 What Is Debounce?
2
🧠 Why Use Debounce?
3
🧪 A Basic Debounce Function
4
✍️ Example: Debounced Input Search
5
⚙️ When Should You Use Debounce?
6
❓ Debounce vs Throttle: What’s the Difference?
7
🧰 Debounce in Utility Libraries
8
✅ Conclusion

Related Posts

How `this` behaves in JavaScript
April 24, 2024
1 min
Š 2025, All Rights Reserved.
Powered By

Quick Links

About Me

Legal Stuff

Social Media