Throttling in JavaScript ⏱🚀
Tags: webdev, javascript, beginners, tutorial
Published: 2024-07-06 18:55:48

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n0htbegijpupsbbuhbtn.png)

---

## How to implement Throttling in JavaScript

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

![basic_function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yol6mf4vg0m7axy5q141.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/42c7rffa3b9rgvmt47vz.png)

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

![throttled_example](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xzlefjpksld8roo7ns6w.png)

---

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1caewa7yqn5dju9g1u73.png)

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

![example_throttling_using_custom_hook](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f9zalhps42buxmj841sr.png)

---

## 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 {% embed https://dev.to/mrhimanshusahni/debouncing-in-javascript-2p3o %}
2. Debouncing vs Throttling {% embed https://dev.to/mrhimanshusahni/debouncing-vs-throttling-2p22 %}

---

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