What is throttling in JavaScript?
I'm a college dropout turned self-taught developer. I transform documented ideas into functioning scalable tech products.
Search for a command to run...
I'm a college dropout turned self-taught developer. I transform documented ideas into functioning scalable tech products.
No comments yet. Be the first to comment.
Is JavaScript compiled language or interpreted language or a mix of both? Don't worry! Take a chill pill 😎 I'll break it down for you, bit by bit. But first thing first... Take a deep breath, and follow along. JavaScript engines use the JIT(just in ...
Have you ever been in a situation where your code runs in one browser and fails in another? And you would be wondering what the hell is happening?? Even after checking your code thoroughly, over and over again, you find it 100% correct. Then where t...
It took me 4-days and 100+ attempts to solve this simple issue of uploading a file using node. And the moment I got it right, I was like...phewww Here is how I solved this issue. By creating a - Simple node server. Middleware to process files. PO...
Whattttt??? Yes, You read that correctly. Let's deep dive into this topic. Before we get into a serious drill, Let's quickly revise objects and functions in JavaScript. Object: Objects in JavaScript are nothing but a non-primitive type data structu...
Again a jargonish word, but let me demystify it for you.

It's nothing but a simple technique we use to prevent unnecessary function calls to improve app's performance.
Unlike Debouncing, here we block a function call for a particular time if it's been executed recently. Or we could also say that throttling ensures a function call regularly at a fixed rate.
Let's look at the below example,
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Throttling</title>
</head>
<body>
<button class="btn">Click here</button>
<script src="index.js"></script>
</body>
</html>
let count = 0;
function submitData(query){
// some api call
console.log("result",count++)
}
document.querySelector(".btn").addEventListener("click",()=>{
submitData();
})

In the above example, if you click 10 times in 2000ms, Function will be called 10 times, as you can clearly see in the above example.
It's a very expensive operation, and as dev, our job is to make operations as cheap as possible.
Let's see how throttling helps us to make these operations cheaper.
function throttle(fn,delay){
let last = 0;
/*
here ...args is optional I've used this in case, if you
want to pass some parameters you can use ...args
*/
return (...args)=>{
const now = new Date().getTime();
if(now - last < delay){
return;
}
last = now;
return fn(...args)
}
}
const magicFunction = throttle(submitData,1000);
let count = 0;
function submitData(){
// some api call
console.log("result",count++)
}
document.querySelector(".btn").addEventListener("click",magicFunction);

Now, if you click 10 times in 2000ms, Function will be called 2 times only, as you can see in the code. We have blocked the function call for 1000ms.
That's how we can implement the throttling technique to improve our app's performance
follow me for more such blog posts.
Let me know if this blog was helpful.