All original content is created in Ukrainian. Not all content has been translated yet. Some posts may only be available in Ukrainian.Learn more

What is debounce in JavaScript and why is it important?

Post cover: What is debounce in JavaScript and why is it important?
Table of contentsClick link to navigate to the desired location
This content has been automatically translated from Ukrainian.
If you have ever worked with events in JavaScript, such as scroll, resize, or keyup, you have probably noticed that they can be triggered very frequently. For example, when typing in a search field, the keyup event may fire after each key press. This can create unnecessary load on the browser or server, especially if each call makes an API request.
To avoid such problems, debounce is used — a technique that limits the frequency of function calls by delaying its execution until the event stops firing for a specified amount of time.

How does debounce work?

The idea is simple: when the event is triggered, we delay the execution of the function for a certain period of time. If the event is triggered again before this time is up, the timer is reset and the countdown starts over. Only when the event stops does the function execute.

Debounce example in JavaScript

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

// Using debounce to handle text input event
const input = document.querySelector("#search");
const handleInput = (event) => {
  console.log("Search: ", event.target.value);
};

input.addEventListener("keyup", debounce(handleInput, 500));
In this example, each input in the search field resets the timer, and only after 500ms without new input does handleInput execute.

Where to use debounce?

  • Search queries — to avoid sending requests to the server on every key press.
  • Window resize handling — to avoid overloading the browser with calculations.
  • Scroll event listeners — for example, to load new content on scroll (infinite scroll).
  • Auto-saving forms — to save data only after input is complete.
  • Form validation - for example, to check for available usernames.
Debounce helps optimize performance by reducing the number of function calls that are frequently triggered. This is especially useful when dealing with frequent events such as text input, scrolling, or window resizing. By using debounce, you can improve user interaction and reduce system load.

This post doesn't have any additions from the author yet.

Virtual cloud server: what it is and what makes it special
03 Jan 10:58

Virtual cloud server: what it is and what makes it special

meme code
meme code@memecode
[Fix] Heroku / SearchBox addon - indexing error "The client is unable to verify that the server is Elasticsearch"
31 Jan 13:09

[Fix] Heroku / SearchBox addon - indexing error "The client is unable to verify that the server is Elasticsearch"

meme code
meme code@memecode
06 Feb 15:31

Fix error [DEPRECATION] #adapters is deprecated. Use #profiles instead. (Codecov / docile)

meme code
meme code@memecode
What is a Promise in JavaScript and how to quickly understand its essence?
18 Feb 11:01

What is a Promise in JavaScript and how to quickly understand its essence?

meme code
meme code@memecode
The structure of Promise (JavaScript) and how to work with it
18 Feb 14:33

The structure of Promise (JavaScript) and how to work with it

meme code
meme code@memecode
What is Memoization (examples in Ruby and Ruby on Rails)?
20 Feb 18:16

What is Memoization (examples in Ruby and Ruby on Rails)?

meme code
meme code@memecode
What is CFB (Cipher Feedback)?
21 Mar 16:53

What is CFB (Cipher Feedback)?

meme code
meme code@memecode
What is XOR and how does it work?
21 Mar 17:05

What is XOR and how does it work?

meme code
meme code@memecode
Embedded programming: what it is and how to get started
24 Mar 16:48

Embedded programming: what it is and how to get started

meme code
meme code@memecode
Pessimistic Lock in Rails: what it is and when to use it. What are the alternatives?
31 Mar 17:45

Pessimistic Lock in Rails: what it is and when to use it. What are the alternatives?

meme code
meme code@memecode
Why does PostgreSQL skip IDs when saving new records? (Heroku)
31 Mar 19:13

Why does PostgreSQL skip IDs when saving new records? (Heroku)

meme code
meme code@memecode
[Codecov] What is the difference between patch and project coverage?
09 Apr 16:03

[Codecov] What is the difference between patch and project coverage?

meme code
meme code@memecode