Debouncing in JavaScript
Tags: javascript, webdev, learning, tutorial
Published: 2024-06-09 10:03:10

## Debouncing in General term

**Debouncing** is a technique in programming that helps prevent time-consuming tasks from being triggered so frequently that they slow down the performance of a web page. In simpler terms, it controls how often a function is called. 

## What is Debouncing in JavaScript? 🤔

In JavaScript, **debouncing** is commonly used to enhance browser performance. Sometimes, certain actions on a web page involve complex computations that take up time. If these actions are triggered too frequently, it can significantly impact the browser's performance, especially since JavaScript operates on a single thread.

> **For Example:** Suppose we have a Search Input on the main page of a website that shows suggestions as the user types. Let's call a function to fetch suggestions on every keystroke. We might end up making too many requests to the backend server, which can slow down the application, waste resources, and, more importantly, affect User Experience. Instead, we can use debouncing to wait until the user has stopped typing for a while before making the request.

## How to implement debouncing in JavaScript? 😮

The most common way to implement debouncing in JavaScript is to use a **wrapper function that returns a new function that delays the execution of the original function.**

> Debouncing accepts a function and transforms it in to an updated (debounced) function so that the code inside the original function is executed after a certain period of time. If the debounced function is called again within that period, the previous timer is reset and a new timer is started for this function call. The process repeats for each function call.

Suppose we have a function `getData()` that needs to execute after say's `300ms`

![getData_Function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ft07neo3zk8h4x3yfsde.png)

Let's implement a debounce wrapper

1. Create a **`index.js`** file

![debounce-wrapper_function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5icw7obznazjy1r72qi9.png)

**How to use debounce function to use with original `getData()` function**

![usage_of_debounce_function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ja85p3gsg7s1r4gy4e8s.png)

## Debounce Implementation using a Custom Hook

> The advantage of custom hooks is that you can reuse the same logic throughout your application. It is highly advisable to do so.


![debounce_with_custom_hook](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/68m3hxepl7zjjj6iq4js.png)

**Usage of debounce in hook with the above example**

![debouce_in_hook_with_example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7cbo83ps7x5d5705yabd.png)


![browser_implementation](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v6srj8cfhie3svnz6m3x.png)

**In the above example:**, we have type **`laptop`** as the input keyword, it has 6 words so 6 times **`onChange`** Event is triggered **_but using the debounce technique we delay this function call after the user has finished typing or delay between 2 key press is greater than delay timer pass in debounce function._**



## Use Case of Debouncing/Real-life examples of Debouncing 

1. **Google Search:** Auto-complete functionality.
2. **Elevator:** When you press the button to call the elevator, it doesn’t move immediately, but waits for a few seconds to see if anyone else wants to get on or off. This way, it avoids going up and down too frequently and saves energy and time.


## Conclusion

> **Debouncing** is a useful technique to optimize web applications by reducing unnecessary or repeated function calls that might affect the performance or user experience.
Debouncing uses the important concept of **_closures_**.

I hope you found this blog helpful and learned something new about Debouncing in JavaScript. 

KeepLearning, Happy_Coding

Thanks for Reading 😀🤩