Logo

dev-resources.site

for different kinds of informations.

Debouncing De-coded

Published at
4/14/2023
Categories
javascript
performance
debounce
react
Author
purohitdheeraj
Author
14 person written this
purohitdheeraj
open
Debouncing De-coded

Hello and welcome to my blog. Today, we are going to write a debounce function from scratch and I will explain the code line by line.

Definition

Debouncing is a technique used to optimize asynchronous operations, such as fetch calls to APIs and scroll events. In simple terms, debouncing delays the processing of an event.

Input Element without Debouncing

The following code demonstrates an input element without debouncing:

Simple Input

For every user input (keyup/keydown) event, the event listener ("onClick") calls the event handler function (in our case, the handleChange function). For a small application, this won't cause any performance delays, but for large data websites like Flipkart and Meesho, the repeated function call can cause performance delays.

Optimization

To optimize the input element, we can implement the following changes:

  1. Instead of setting the event value on every change, we can delay the process of setting the event value by a certain waiting time (in milliseconds).
  2. Inside the handleChange function, we are setting the input value on every user input. However, we just need to delay the process of setting that event value to the query (setQuery(e.target.value)).

Debounce Implementation

We are going to build the debounce function step by step. The first step is to delay the function call. To do this, we can use the timer function. However, we need to make a function call after some delay, which is where the setTimeout function comes in.

Here is the code implementation following the above steps:

 const debounce = (cb, wait) => {
    let timer;
    return function (...args) {
        clearTimeout(timer);
        timer = setTimeout(() => {
            cb(...args);
        }, wait);
    };
  };
Enter fullscreen mode Exit fullscreen mode

Here is a breakdown of what each line does:

  1. The debounce function returns a function, as it will be called multiple times.
  2. The debounce function accepts a callback function and a wait time for delay.
  3. As discussed, we are delaying the callback function execution call by the wait time .
  4. The callback can be a fetch request or a setter function.
  5. To execute the callback, we need to make sure previous setTimeouts are cleared .
  6. The user can pass another event before the previous timer gets completed, so we need to clear the timer to handle that case.

Input Element with Debouncing

Here is an example of what the input element looks like with debouncing:

With debouncing

We can see that there is a slight delay.
we achieved the goal

That brings an end to this blog. Thanks for reading and happy coding!

debounce Article's
30 articles in total
Favicon
If you think debounce or onblur is not working in React, you can check it.
Favicon
Enhance Your React Native App with Debouncing for API Optimization
Favicon
Dboun
Favicon
React: Debouncing input with useEffect
Favicon
Debounce Method for Searching
Favicon
Optimizing React Performance with useDebounce.
Favicon
Understanding JavaScript Debounce and Throttling
Favicon
Debounce function with a return value using promises
Favicon
Introducing dwayne/elm-debouncer: Debounce or throttle your actions in Elm
Favicon
Debouncing De-coded
Favicon
Some Problems I Face When Building React Single Page Application And How I Solve Them
Favicon
Debounce Input in React
Favicon
Como aplicar debounce em input de busca sem lodash
Favicon
Debouncing: how to use it
Favicon
Debounce in Next.js
Favicon
Optimized React Search Bar πŸ”―
Favicon
Learn Debounce and Throttle
Favicon
Π˜ΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅ΠΌ Throttle ΠΈ Debounce Π² React
Favicon
Debouncing in Javascript using a custom function or Lodash library.
Favicon
Debounce Function in Javascript πŸš€
Favicon
Debouncing TypeScript
Favicon
How to use Lodash debounce method?
Favicon
Debounce in JavaScript
Favicon
What is Debouncing? Search Suggestions - A Javascript Implementation
Favicon
Throttle and Debounce in React
Favicon
Debounce and Throttle
Favicon
useDebounce hook in React, Improve performance of your React app with Debouncing
Favicon
Debouncing (lodash) with React
Favicon
Use lodash.debounce inside a function component in React
Favicon
How to Debounce UIButton Taps

Featured ones: