React.js: debouncing and throttling

Introduction

Hello, community 🙌🏻,

In order to build a professional web application, optimization and performance are two important things you need to care about.

There are many tips and techniques used to increase the performance of a web application, such as Debouncing and Throttling.

When it comes to debouncing and throttling, developers often confuse.

During this blog, I will go throw these two techniques in details using react.js, but it is the same principle for vanilla JavaScript or any other JavaScript framework.

Debouncing

Before dive deep into debouncing, let's see a simple and normal example that implement a search box that allows users to search something without clicking any button.

1function App() { 2 const handleChange = (e) => { 3 console.log("api call..."); 4 }; 5 6 return ( 7 <div className="App"> 8 <header className="App-header"> 9 <p> Search </p> 10 <input type="text" onChange={handleChange} /> 11 </header> 12 </div> 13 ); 14}

The issue is that handleChange is very expensive, and this is bad to the server because it will receive many HTTP requests in the same time.

Maple

To solve the problem, we need to use a debounce function.

Definition and implementation of a debounce function A debounce function is called after a specific amount of time passes since its last call.

1function debounce(fn, delay) { 2 let timer 3 return function (...args) { 4 clearTimeout(timer) 5 timer = setTimeout(()=>fn(...args), delay) 6 }

The idea is to define a high-order function called debounce takes as arguments a callback function and a delay in ms, then returns a new function that sets the timer to execute the callback after the timer done. The secret here is that every call of the function returned from the debounce function will cancel the previous timer using cleartimeout(timer) and start a new timer. With this approach, we can be sure that the callback function will be executed just once after the time that we passed as an argument in the last call.

Implement debounce function into our example

1<div className="App"> 2 <header className="App-header"> 3 <p> Search </p> 4 <input type="text" onChange={debounce(handleChange, 500)} /> 5 </header> 6</div>

Result

Maple

Throttling

Let's assume that we have an event listener in our app to track the movement of the user mouse, then send data to a backend server to do some operations based on the location of the mouse.

1const handleMouseMove = (e) => { 2 //everytime the mouse moved this function will be invoked 3 console.log("api call to do some operations..."); 4}; 5//event listener to track the movement of the mouse 6window.addEventListener("mousemove", handleMouseMove);

If we stick with this solution, we will end up with a down backend server because it will receive a hundred of requests in short duration.

Maple

1600 API calls in few seconds is very very bad 📛📛📛. To fix this issue, we need to limit the number of API calls, and this kind of problems can be solved using a throttle function.

Definition and implementation of a throttle function

A throttle function is a mechanism to limit the number of calls of another function in a specific interval, any additional calls within the specified time interval will be ignored.

1function throttle(fn, delay) { 2 let run = false; 3 return function (...args) { 4 if (!run) { 5 fn(...args); 6 run = true; 7 setTimeout(() => (run = false), delay); 8 } 9 }; 10}

The throttle function accepts two arguments: fn, which is a function to throttle, and delay in ms of the throttling interval and returns a throttled function.

Implement throttle function into our example

1const handleMouseMove = (e) => { 2 //everytime the mouse moved this function will be invoked 3 console.log("api call to do some operations..."); 4}; 5 6//event listener to track the movement of the mouse 7window.addEventListener("mousemove", throttle(handleMouseMove, 1000)); 8//1000ms => 1 second

Result

Maple

Conclusion

Debouncing and Throttling are two amazing techniques, they can increase the performance of your web application to another level. Choosing one of them depends on the case.

GitHub repo: https://github.com/ridhamz/debouncing-throttling