What is debouncing in JavaScript?
I'm a college dropout turned self-taught developer. I transform documented ideas into functioning scalable tech products.
Search for a command to run...
I'm a college dropout turned self-taught developer. I transform documented ideas into functioning scalable tech products.
No comments yet. Be the first to comment.
Is JavaScript compiled language or interpreted language or a mix of both? Don't worry! Take a chill pill 😎 I'll break it down for you, bit by bit. But first thing first... Take a deep breath, and follow along. JavaScript engines use the JIT(just in ...
Have you ever been in a situation where your code runs in one browser and fails in another? And you would be wondering what the hell is happening?? Even after checking your code thoroughly, over and over again, you find it 100% correct. Then where t...
It took me 4-days and 100+ attempts to solve this simple issue of uploading a file using node. And the moment I got it right, I was like...phewww Here is how I solved this issue. By creating a - Simple node server. Middleware to process files. PO...
Whattttt??? Yes, You read that correctly. Let's deep dive into this topic. Before we get into a serious drill, Let's quickly revise objects and functions in JavaScript. Object: Objects in JavaScript are nothing but a non-primitive type data structu...
Again a jargonish word, but let me demystify it for you. It's nothing but a simple technique we use to prevent unnecessary function calls to improve app's performance. Unlike Debouncing, here we block a function call for a particular time if it's be...
While building an app using JavaScript. We may need to call the function many times to achieve the desired result.
Some examples are

Let's see, how we usually write code to achieve the desired result.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Debouncing</title>
</head>
<body>
<input class="search" type="text" placeholder="search"/>
<script src="index.js"></script>
</body>
</html>
let count = 0;
function search(query){
// some api call
console.log("result",count++)
}
document.querySelector(".search").addEventListener("input",(e)=>{
search(e.target.value);
})

In the above example, If I search for a laptop, The function search will be called 6 times. Which is an inefficient way of calling a function. And it is very expensive for JavaScript.
You must be wondering what do you mean by expensive for JavaScript?

JavaScript is a synchronous single-threaded language. Which means we have a single callstack to execute everything. And because it is single-threaded, we should not block the thread by calling functions unnecessary.

But wait ...
How do we prevent unnecessary function calls to improve performance?

Here, the concept of debouncing kicks into the picture.

Debouncing is a technique used to improve the browser's performance. It ensures that time-consuming tasks do not fire so often, In other words, it limits the rate at which a function gets invoked.
Let's re-write the above code using Debouncing technique.
let count = 0;
function search(query){
// some api call
console.log("result",count++)
}
const magicFunction = debounce(search,300);
function debounce(func, delay){
let timer;
return function(...args) {
clearTimeout(timer);
timer = setTimeout(() => {
func(...args);
}, delay);
};
}
document.querySelector(".search").addEventListener("input",(e)=>{
magicFunction(e.target.value);
});

The idea behind debouncing technique is that we do not call function during the event but after the event.
In the above example, We can clearly see.
debounce is a higher order function(HOF). Which takes two parameters the first parameter will be a function and the second will be a delay.
If the delay between two keystrokes is less than 300ms, function will not execute and setTimeout will be destroyed. And if the delay is more than 300ms, then only function will execute.
That's how we can implement the debouncing technique to improve our app's performance
follow me for more such blog posts.
Let me know if this blog was helpful.