DEV Community

Cover image for Throttling in JavaScript ⏱🚀
Himanshu Sahni
Himanshu Sahni

Posted on

Throttling in JavaScript ⏱🚀

As a developer, making your website user-friendly is important. This goes a long way toward the product's success, and a key part of the user experience is the website's performance.


Table of Contents 📃

  1. What is Throttling in JavaScript?
  2. How to implement Throttling in JavaScript
  3. Implement Throttling using a Custom Hook
  4. Implementations of throttling in JavaScript libraries
  5. Why use Throttling?
  6. Use Cases for Throttling

What is Throttling? 🙄

Throttling is a technique that limits how often a function can be called in a given period of time. It is useful for improving the performance and responsiveness of web pages that have event listeners that trigger heavy or expensive operations, such as animations, scrolling, resizing, fetching data, etc.

For example, if you have a function that fetches some data from an API every time the user scrolls the page, you might want to throttle it so that it only makes one request every second, instead of making hundreds of requests as the user scrolls. This way, you can avoid overloading the server or the browser with unnecessary requests and reduce the bandwidth consumption.


Pictorial Representation

pictorial_representation


How to implement Throttling in JavaScript

Let's Take a function myFunction(a, b) having two arguments.

basic_function

We want to modify this function so that it can only be called once in 500ms. So, throttling will take myFunction() as an input, and return a modified (throttled) function throttledFun() that can only be executed 500ms after the previous function was executed.

throttling_code

By using the above throttled function, we now can have throttledFun() based on myFunction().

throttled_example


Implement Throttling using a Custom Hook

  1. Create a new file called as useThrottle.js.
  2. Add the below custom function that we have made above.

throttling_using_custom_hook

Usage of throttling and myFunction() with custom throttled hook.

example_throttling_using_custom_hook


Throttling & Debouncing with JavaScript libraries

Libraries such as Lodash offer _.debounce and _.throttle functions, providing a more robust and cross-browser compatible solution.

Why use Throttling?

  1. Performance Enhancement: Reduces the number of function executions, prevents Overloading the server or the browser with too many requests or calculations.
  2. Resource Management: To manage the bandwidth or resources on operations that are not visible or relevant to the user.
  3. Consistent Updates: Ensures regular updates at specified intervals, useful for tasks like updating a position indicator during scrolling.

Use Cases for Throttling

  1. Scroll event listeners: Many web applications utilize a scroll event listener to keep track of the scroll position and load or animate the content appropriately. In these cases, the scroll event may have a negative performance impact if we scroll it too frequently since it contains many videos and images. Thus, we must use throttling for the scroll event.

  2. In Gaming: In games, a player needs to push the button to trigger any action, such as punching, shooting but during playtime, the user can hit the button more than one time in a second, and we want to trigger an action at the rate per second then the concept of Throttling will be useful for it.

  3. API Calls: In some cases, we may wish to limit how frequently our application calls an external API. In this case, throttling can be advantageous. By rate limiting the calling function, it would eliminate unnecessary API requests to the server.


Conclusion

Throttling is a powerful technique for optimizing web applications by controlling the frequency of function executions. By ensuring functions are called at regular intervals, throttling helps manage performance and resource usage effectively. Whether dealing with scrolling, resizing, or other rapid events, throttling is an essential tool in your JavaScript toolkit.


I hope you found this blog helpful and learned something new about throttling in JavaScript.

You can check out my recent articles on

  1. Debouncing in JavaScirpt
  2. Debouncing vs Throttling

I hope you liked this article 😀
Happy_Coding 😎

Follow me for more tutorials and tips on web development. Feel free to leave comments or questions below!

Follow and Subscribe 🤗

Twitter - https://twitter.com/mrHimanshuSahni
Linkedin - https://www.linkedin.com/in/mrhimanshusahni/
Github - https://github.com/mrhimanshusahni
Youtube - https://www.youtube.com/@mrhimanshusahni

Top comments (0)