Debouncing and Throttling

UI Engineer @Flipkart I like to explore new technologies & build projects on them.
Search for a command to run...

UI Engineer @Flipkart I like to explore new technologies & build projects on them.
No comments yet. Be the first to comment.
Let's enable infinite scroll for virtual scrollbars

Event Bubbling, Event Capturing, Event Delegation

Introduction One of the reasons React is so popular is that it's blazing fast. This speed is achieved by updating only part of the real DOM that has changed. But, updating the real DOM is a slow process. So, how does React achieve this? React achieve...

Javascript is a single-threaded interpreted language with a non-blocking event loop. What this means is that everything runs on a single main thread so it's important to use the thread only when required. Blocking this main thread means subsequent operations have to wait till the main thread is clear, this may result in performance issues.
In this blog, we will look at 2 simple examples to understand what are these issues & how to tackle them thereby optimizing performance. Let's say we have an expensive function, a function that makes an API call and performs some heavy computation and then some significant UI changes.
Consider an input search field with this expensive function attached to its onChange handler. Say, I want to search for a "refrigerator" and I type this without making any wrong keystrokes. In this case, the expensive function will be called 12 times. You might argue that it is better to call the function as suggestions can be quicker. Now, Imagine 1 million users search something at the same time. Imagine the number of requests a server gets that too when someone doesn't make a mistake in typing. So, as the number increases, the number of requests also increases. But do we need to call this function on every change? We probably don't, we can optimize this by calling the function if the delay between two events(keystrokes) is more than let's say 400ms. In simple words, if the user pauses while typing call the function. This method of performance optimization is called debouncing.
const debounce = (func, delay) => {
let timer;
return function () {
clearTimeout(timer);
timer = setTimeout(() => func(), delay);
};
};
Try searching with and without debounce and notice the number of API calls -
Consider a shooting game where to fire a weapon we need to click and this expensive function is attached to its onClick handler. In an intense fighting situation where we want to kill the enemies, we would spam the clicks to fire as many rounds as possible. But, a weapon has its limits - Let's say between subsequent shots, there is a delay of 300ms. But, we won't think of all this we would spam the click. So we need to call the function only where there is a delay of 400ms from the last function call. This method of rate-limiting of the function call is called throttling.
const throttle = (func, limit) => {
let flag = true;
return function () {
if (flag) {
func();
flag = false;
setTimeout(() => (flag = true), limit);
}
};
};
Try Spamming the two fire buttons and notice the number of shots fired -
DEBOUNCING => call a function if the delay between events is more than d THROTTLING => call a function if the delay between function call is more than d
Note: When it comes to debouncing vs throttling, there is no clear winner, it depends on the situation.