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 📃
- What is Throttling in JavaScript?
- How to implement Throttling in JavaScript
- Implement Throttling using a Custom Hook
- Implementations of throttling in JavaScript libraries
- Why use Throttling?
- 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
How to implement Throttling in JavaScript
Let's Take a function myFunction(a, b)
having two arguments.
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.
By using the above throttled function, we now can have throttledFun()
based on myFunction()
.
Implement Throttling using a Custom Hook
- Create a new file called as
useThrottle.js
. - Add the below custom function that we have made above.
Usage of throttling and myFunction()
with custom throttled 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?
- Performance Enhancement: Reduces the number of function executions, prevents Overloading the server or the browser with too many requests or calculations.
- Resource Management: To manage the bandwidth or resources on operations that are not visible or relevant to the user.
- Consistent Updates: Ensures regular updates at specified intervals, useful for tasks like updating a position indicator during scrolling.
Use Cases for Throttling
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.
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.
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
Debouncing in JavaScirpt %[dev.to/mrhimanshusahni/debouncing-in-javasc..
Debouncing vs Throttling %[dev.to/mrhimanshusahni/debouncing-vs-thrott...
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 - twitter.com/mrHimanshuSahni
Linkedin - linkedin.com/in/mrhimanshusahni
Github - github.com/mrhimanshusahni
Youtube - youtube.com/@mrhimanshusahni