What is Throttling in JavaScript - Implementation & Examples
Understand the concept of throttling in JavaScript to improve the performance of your web application and optimize the event handling in JavaScript, by limiting API calls or DOM events.
Anuj Sharma
Last Updated Jun 4, 2026

Throttling in javascript is a very frequently used technique nowadays to improve performance by limiting the number of calls when the user uses any web application and there is a function call which used to happen on an event, In this case, we can't control the user behaviour but behind the scenes, we can limit the function calls so that there won't be any performance issue. Let's see the implementation and how it works.
Table of content
Throttling in JavaScript
Throttling is a technique in JavaScript that limits the number of function calls in a certain interval. This technique can also help in improving web application performance by limiting the number of function calls in cases where a frequent number of calls are happening.
Throttling can also be used to limit the function callbacks associated with the events in javascript, there are a few types of events that generate a large number of callback function calls when events happen for example scrolling and window resize events. These events generate an event call back for each pixel change, but in practical scenarios, there is no requirement to call a function on every pixel change.
In these cases, throttling can be used to limit the number of function calls on event change and it is helpful to optimize event handling in JavaScript.
Throttling Implementation in JavaScript
The throttle function takes a callback function and an interval, it returns the throttled function. Now the throttled function allows to invoke the actual function once during the mentioned time interval. It doesn't matter how many times the function got involved during the interview but the function will run only once.
Explain the throttling implementation step by step
- The throttling function requires a flag to limit the calls, and this flag is initialized
trueto make sure it will run the first time immediately when the function is called. - The throttling function returned the throttled function and required function context and parameters to invoke the function using apply (since arguments are array-like objects).
- After the invocation, the flag is set to
falseand have a setTimout logic to make sure the flag will turntrueonly after the mentionedinput interval.
// ā” Throttling Implementation Code
const throttle = function (callback, interval) {
// True allows function call for the first time
let isCallAvailable = true;
// It returns a new throttled function
return function () {
// Captures context and arguments from the throttled function
const context = this;
const args = arguments;
// Limit the function call through flag.
if (isCallAvailable) {
// Called throttled function
callback.apply(context, args);
isCallAvailable = false;
// Set timer to limit the availability of function call through flag for a certain interval
setTimeout(() => {
isCallAvailable = true;
}, interval);
}
};
};
How to use the throttle function.
function throttleExample() {
console.log('Throttling Example');
}
/*
The new throttledFunction will limit the call once in 5000 secs
even though there are many function calls during this interval.
*/
const throttledFunction = throttle(throttleExample, 5000);
Throttling Use-Case Examples
1. Throttle Resize Event Callback
Throttling the callback function limits the function call registered with the Resize event.
let counter = 0;
let throttleCount = 0;
// function without throttling
const withoutThrottle = function () {
counter++;
console.log("resize window...", counter);
};
// throttled function
const resizeWindow = function () {
throttleCount++;
console.log("throttled resize window...", throttleCount);
};
const ThrottledFunction = throttle(resizeWindow, 1000);
window.addEventListener("resize", withoutThrottle);
window.addEventListener("resize", ThrottledFunction);
Output
2. Throttle Scrolling Event Callback
Throttling the callback that limits the function call registered with the Scrolling event.
/*
For the scroll event to work, there should be a scroll bar
<div style="height: 500px" id="app"></div>
*/
let counter = 0;
let throttleCount = 0;
// function without throttling
const withoutThrottle = function () {
counter++;
console.log("scroll window...", counter);
};
// throttled function
const scrollWindow = function () {
throttleCount++;
console.log("throttled scroll window...", throttleCount);
};
const ThrottledFunction = throttle(scrollWindow, 1000);
window.addEventListener("scroll", withoutThrottle);
window.addEventListener("scroll", ThrottledFunction);
Output
Final Note
Throttling in javascript is a very important technique to know, in order to improve performance by limiting API calls to the server or optimizing event handling in the web application. "How to limit a function call, when the function call invocation is happening very frequently" is a very common frontend interview question to limit the function calls based on the throttling technique, that function can have any specific job, like sending analytics to the server, logging data to the server, changing UI effect etc.
Hope this post helps you to understand the throttling in JavaScript very effectively. Happy Coding :)
ā© Next, Read
A seasoned Sr. Engineering Manager at GoDaddy (Ex-Dell) with over 12+ years of experience in the frontend technologies. A frontend tech enthusiast passionate building SaaS application to solve problem. Know more about me š
Learn Next
Comments
Be the first to share your thoughts!
No comments yet.
Start the conversation!
Share your expertise
Publish a blog or quick notes on topics you know well ā your write-up could be the answer someone needs before their next frontend interview.
Build your portfolio
Help the community
Sharpen your skills
Earn goodies
Other Related Blogs
Understand Debouncing in JavaScript with Examples
Anuj Sharma
Last Updated Jun 27, 2026
Understand the concept of Debouncing in JavaScript to improve the performance of your web application and optimize the event handling in JavaScript, by limiting API calls or DOM events.
How to convert HEX to RGB in JavaScript
Anuj Sharma
Last Updated Jul 6, 2026
A quick way to convert HEX to RGB in JavaScript that will help to do the CSS HEX to RGB color conversion programmatically.
How to convert RGB to HEX in JavaScript
Anuj Sharma
Last Updated Jul 6, 2026
A quick way to convert RGB to HEX in JavaScript that will help to do the CSS RGB to HEX color conversion programmatically
Implement JSON Parse polyfill in JavaScript
Anuj Sharma
Last Updated Jun 15, 2026
Explore the code implementation of JSON Parse Polyfill in JavaScript that can parse a string into valid JSON.
Implement JSON Stringify Polyfill in JavaScript
Anuj Sharma
Last Updated Jun 15, 2026
Explore the detailed explanation of the JSON Stringify Polyfill implementation in JavaScript to prepare for the frontend interviews.
What is javascript:void(0) and How it Works?
Anuj Sharma
Last Updated Jun 15, 2026
A comprehensive explanation about using javascript:void(0) in javascript. When to use javascript:void(0) and how it works with examples of using it with anchor tag.
Understand JavaScript Date Object with Examples (for JavaScript Interviews)
Anuj Sharma
Last Updated Jun 15, 2026
Go through different ways to display dates using javascript date object. It covers examples of date object usage to understand the main concepts of javascript date object.
Apply Polyfill in JavaScript: Step by Step Explanation (For Interview)
Anuj Sharma
Last Updated Jun 15, 2026
Understand how the apply method works in JavaScript and cover step by step-by-step explanation of the apply method polyfill in JavaScript to understand its internal implementation.
