In this article, we will check what is throttling and how it is different than debouncing.
Before that, if you don't know what is debouncing then,
I will suggest you read this article on Debouncing(UI interview question for Flipkart).
Throttling is a technique to control how many times a function to be executed over time.
Why it is important?
In 2011, Issue in the Twitter website,
when you were scrolling down your Twitter feed, it became slow and unresponsive.
John Resig published a blog post about the problem where it was explained how bad of an idea
it is to directly attach expensive functions to the scroll event.
How it is different from Debouncing?
Well, the debouncing technique is useful for example like search bar in Flipkart, Amazon where we limit the API call to the server. If we are searching for some product and when we take pause(200ms) the API will get called.
(Only give an API call if the difference between 2 keypress event is some x seconds.)
Consider a use case where we are building a shooting game and there are 2 weapons(Guns)
like the machine gun and normal pistol.
Also, there is a fire button for these 2 weapons which fires a bullet.
Users will be constantly clicking the button.
Here we can use throttling.
Like fire function for a machine gun(As machine guns fire quickly) will be called only after 100ms with respective to previous call but the same function for pistol will be called after 800ms delay.
Here we can't use the debouncing approach.
Both the technique is used for controlling the expensive call to server but are used in different use cases.
Link to the code.
You can also follow me on Twitter or Linkedin for the latest updates.