Debouncing in JavaScript (original) (raw)

Last Updated : 15 Feb, 2025

In JavaScript, debouncing is a technique used to ensure that a function is not called too frequently. It is commonly used in scenarios where events are triggered rapidly, such as typing in an input field or resizing a window. Without debouncing, functions might be executed many times in quick succession, causing performance issues or unwanted behaviour.

What is Debouncing in JavaScript?

Debouncing in JavaScript can be defined as the technique that is used to limit the number of times a function gets executed. Debouncing is useful when the event is frequently being triggered in a short interval of time like typing, scrolling, and resizing.

// Debounce function function debounce(func, delay) { let timeout; return function (...args) { clearTimeout(timeout); timeout = setTimeout(() => { func.apply(this, args); }, delay); }; }

// Function to be debounced function search(query) { console.log('Searching for:', query); }

// Create a debounced version of the search function const dSearch = debounce(search, 100);

// Simulate typing with multiple calls to the debounced function dSearch('Hello'); dSearch('Hello, '); dSearch('Hello, World!'); // Only this call will trigger after 100ms

`

Output

Searching for: Hello, World!

**In this example

How Does Debouncing Work?

In JavaScript the debouncing function works when the event is being triggered. The Debounce wait for the specific period to run the function, it doesn't run the function immediately. If before the wait time is over, the event is triggered again then the previous function call is canceled and it resets the timer. Once the timer completes without any further event triggers, the function is executed. This ensures that the function is executed only after the event stops occurring for a specific period.

Use Cases for Debouncing in JavaScript

The use cases of the debouncing in javaScript are mentioned below:

let timer; document.getElementById("searchInput").addEventListener("input", () => { clearTimeout(timer); timer = setTimeout(() => console.log("Searching..."), 300); });

let timer; window.addEventListener("resize", () => { clearTimeout(timer); timer = setTimeout(() => console.log("Window resized"), 500); });

let timer; window.addEventListener("scroll", () => { clearTimeout(timer); timer = setTimeout(() => console.log("Scrolling stopped"), 300); });

let timer;
document.getElementById("formInput").addEventListener("input", () => {
clearTimeout(timer);
timer = setTimeout(() => console.log("Validating..."), 500);
});

Benefits of Debouncing

Debouncing vs Throttling

Debouncing and Throttling both are used for limiting the function calls during an event, but they both work in different ways:

Features Debouncing Throttling
Definition Executes a function only after a specified delay with no further events during that time. Executes a function at regular intervals, no matter how frequently the event occurs.
Execution Trigger After the event stops firing for a set time. At fixed intervals, regardless of the event frequency.
Delay/Interval Delays the function call until the event stops. Limits the function call to a specific interval, regardless of continuous events.
Function Calls The function is called once after the event stops firing for a defined time. The function is called every X milliseconds, even if the event triggers more frequently.
Example Typing in a search box Scroll event

When to Use Debouncing

We can use the debouncing in the following conditions: