Learn
Turtle
Debounce and Throttle
Debounce and Throttle

Debounce and Throttle

Some things that you need to know before you start playing around with production

Why debounce or throttle?

Have you ever used a browser event such as onscroll or onresize in a web app? If you did, you certainly would have noticed how the app heavily slows down if you call anything that requires even a little bit of extra processing juice. This obviously happens because the said code is numerously called inside the event's callback for a lot more number of times than it actually should be for optimal performance. Debouncing and throttling are two major concepts that can help you to solve this issue. Read along to find out what they are!

image

Debouncing

Debouncing is a concept often used in functional programming which helps to delay a callback after it has been triggered. It is due to this concept that when you type in a search box which does not require you to press the seach button, you still face a delay of at least a fraction of a second before you get to see the results. This delay is intentionally placed by a developer so that the target function, which generally calls a paid-for remote API or carries out a performance intensive task, does not get called so frequently as to overload the costs/resources.

You can see it in action here -

Err.. How do I use it?

To implement debouncing, you can use Underscore or lodash's inbuilt function called debounce. Or if you feel using an entire library just for one function is an overkill, you could extract it. Its usage is pretty simple and straightforward -

window.onscroll = _.debounce(() => {
    // Make requests to remote APIs here...
    // Note that the _ before debounce stands as an reference of lodash
}, 100);

Sounds straightforward, doesn't it?

image

Throttle

Throttle is another concept used to tackle the same problem. More often than not, onscroll events are used to lazily-load items in a list, or to update a progress bar that shows how far you've scrolled down an article. In such cases, delaying the UI update until the input stops can relay a bad user experience. So instead of waiting until the user completes the input, the callback function is fired at fixed intervals, so that the UI keeps getting updated. This means that the function will still be called many times, however, certainly not as many times as it would have without throttling down.

How to throttle down?

Again, both Underscore & lodash have an inbuilt function for this, which you can either use by importing the libraries or extracting the functions. Here's a sample implementation -

window.onscroll = _.throttle(() => {
    // Perform resource-intensive tasks here...
    // Note that the _ before throttle stands as an reference of lodash
}, 100);

Everything together in action

Here's a codepen that contrasts the output of all the three methods of handling callbacks -

Footnotes

Underscore is available as an npm package for node based projects and can be installed by -

npm install underscore

lodash too can be installed via npm

npm install lodash

Kumar Harsh
June 01, 2020
SDE Intern | Student Mentor | Tech Writer | MERN | Mobile | Gratitude 🍁
Read More

Read 0 times

Comment!