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.
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.
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.
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
Result
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.
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.
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.
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
Result
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