What Exactly Throttling Is, How It Works, Why You Need Throttling & How To Implement Throttling In Plain Javascript.

If You are a web developer, you might have come across the term “Throttling”. It is a technique very often used by programmers & very useful to improve some performance. In this article, you will learn What is throttling, how throttling works & how you can implement it in pure javascript & we will see some of its use cases. So without delay let’s get started.

What Is Throttling ?

 “ In Programming Throttling Is Technique To Limit The Number Of Execution In Given Time.”

Explanation Of How Throttling Works 

 For example, we call a function when the user clicks the button. if a user clicks button 500 times, function will be called 500 times. but what if we want to limit the function calls. for example, no matter how many times a user clicks the button, we want to call the function only certain number of times.in such situations, we can use a technique called throttling.

Why We Need Throttling

Executing heavy operation every time degrades the performance of the software. Throttling prevents execution every time a call is made. Only executes function if limit matches. That can significantly improve performance & you can give a better user experience.

Example Of Implementing Throttling In Javascript

Explanation Of Above Example

In the above example, if the user clicks the button we call the HeavyTask function. but if the user clicks again then we call the HeavyTask function only if the time difference between the last call & the current call is more than 1 second. so no matter how many times the user clicks the button we call HeavyTask once every 1 second.

When To Use Throttling Or Some Use Cases Of Throttling

  • Animation
  • Network request
  • Scroll events
  • Event Handling
  • Gaming Features

Implement  Throttling in various javascript libraries

If you use libraries then you don’t need to write your own code. A library like jquery, lodash, underscore comes with its own throttling implementation.

Implementing throttling in jquery


Implementing throttling in lodash


Implementing throttling in underscore


End Note:

So in Short throttling limits the number of execution. I hope the concept Of Throttling is now clear to you. If you have any doubt, suggestions or feedback feel free to comment below or contact me on my email.

    Leave a Reply

    Your email address will not be published.