<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Understanding Throttling in JavaScript]]></title><description><![CDATA[Understanding Throttling in JavaScript]]></description><link>https://understanding-throttling-in-javascript.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Wed, 14 Oct 2026 09:18:51 GMT</lastBuildDate><atom:link href="https://understanding-throttling-in-javascript.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[JavaScript Throttling Explained]]></title><description><![CDATA[Master JavaScript Throttling: Improve Performance & Avoid Lag
What is Throttling?
Throttling is a way to limit how often a function runs. It makes sure the function runs only once in a set time, even if an event happens many times. This helps prevent...]]></description><link>https://understanding-throttling-in-javascript.hashnode.dev/javascript-throttling-explained</link><guid isPermaLink="true">https://understanding-throttling-in-javascript.hashnode.dev/javascript-throttling-explained</guid><category><![CDATA[throttling in javascript]]></category><category><![CDATA[throttling]]></category><dc:creator><![CDATA[Shahbaz Alam]]></dc:creator><pubDate>Sat, 15 Mar 2025 05:48:39 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1742016247416/b654ba8a-1f48-4503-b2ab-d872acb7fb74.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<h3 id="heading-master-javascript-throttling-improve-performance-amp-avoid-lag"><strong>Master JavaScript Throttling: Improve Performance &amp; Avoid Lag</strong></h3>
<h2 id="heading-what-is-throttling">What is Throttling?</h2>
<p>Throttling is a way to <strong>limit how often</strong> a function runs. It makes sure the function runs <strong>only once</strong> in a set time, even if an event happens many times. This helps prevent unnecessary function calls and keeps things running smoothly.</p>
<h2 id="heading-why-use-throttling">Why Use Throttling?</h2>
<p>Throttling is particularly useful for optimizing performance in scenarios where events fire repeatedly in quick succession, such as:</p>
<ul>
<li><p>Scrolling events</p>
</li>
<li><p>Window resizing</p>
</li>
<li><p>Button clicks</p>
</li>
<li><p>API calls</p>
</li>
</ul>
<p>By throttling a function, we can improve application efficiency and prevent performance issues like excessive rendering or unnecessary API requests.</p>
<h3 id="heading-implementation-of-throttling-in-javascript">Implementation of Throttling in JavaScript</h3>
<pre><code class="lang-javascript"><span class="hljs-comment">// Throttle function: Ensures `fn` is called at most once every `delay` milliseconds.</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">throttle</span>(<span class="hljs-params">fn, delay</span>) </span>{
    <span class="hljs-keyword">let</span> lastCall = <span class="hljs-number">0</span>; <span class="hljs-comment">// Tracks the last execution time</span>

    <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">...args</span>) </span>{
        <span class="hljs-keyword">const</span> now = <span class="hljs-built_in">Date</span>.now(); <span class="hljs-comment">// Get current timestamp</span>
        <span class="hljs-keyword">if</span> (now - lastCall &gt;= delay) { <span class="hljs-comment">// Check if enough time has passed since last call</span>
            lastCall = now; <span class="hljs-comment">// Update last execution time</span>
            fn(...args); <span class="hljs-comment">// Call the original function with the provided arguments</span>
        }
    };
}

<span class="hljs-comment">// Function to simulate sending a chat message</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sendChatMessage</span>(<span class="hljs-params">message</span>) </span>{
    <span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Sending Message:'</span>, message);
}

<span class="hljs-comment">// Create a throttled version of `sendChatMessage` that executes at most once every 2 seconds</span>
<span class="hljs-keyword">const</span> sendChatMessageWithSlowMode = throttle(sendChatMessage, <span class="hljs-number">2000</span>);

<span class="hljs-comment">// Simulating multiple calls</span>
sendChatMessageWithSlowMode(<span class="hljs-string">'Hi'</span>);  <span class="hljs-comment">// Executes immediately</span>
sendChatMessageWithSlowMode(<span class="hljs-string">'Hello'</span>);  <span class="hljs-comment">// Ignored (called too soon)</span>
sendChatMessageWithSlowMode(<span class="hljs-string">'Hello ji'</span>);  <span class="hljs-comment">// Ignored (called too soon)</span>
<span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> sendChatMessageWithSlowMode(<span class="hljs-string">'Hello again'</span>), <span class="hljs-number">3000</span>);  <span class="hljs-comment">// Executes after 3 seconds</span>
</code></pre>
<blockquote>
<p><strong>Output</strong><br />Sending Message: Hi</p>
<p>(3 seconds delay...)</p>
<p>Sending Message: Hello again</p>
</blockquote>
<h3 id="heading-step-by-step-execution-flow">Step-by-Step Execution Flow</h3>
<ol>
<li><p><strong>Throttle Function Call:</strong></p>
<ul>
<li><p><code>throttle(sendChatMessage, 2000)</code> is called.</p>
</li>
<li><p>The throttle function executes immediately and returns a new function, which we store in <code>sendChatMessageWithSlowMode</code>.</p>
</li>
</ul>
</li>
<li><p><strong>First Function Execution:</strong></p>
<ul>
<li><p><code>sendChatMessageWithSlowMode('Hi')</code> is called.</p>
</li>
<li><p>Inside the throttled function:</p>
<ul>
<li><p><code>now = Date.now()</code> stores the current timestamp.</p>
</li>
<li><p><code>lastCall</code> is <code>0</code>, so <code>now - lastCall</code> is greater than <code>delay (2000ms)</code>.</p>
</li>
<li><p><code>lastCall</code> is updated to <code>now</code>.</p>
</li>
<li><p><code>sendChatMessage('Hi')</code> is executed.</p>
</li>
</ul>
</li>
</ul>
</li>
<li><p><strong>Second Function Call (Ignored):</strong></p>
<ul>
<li><p><code>sendChatMessageWithSlowMode('Hello')</code> is called immediately after.</p>
</li>
<li><p><code>now - lastCall</code> is less than <code>2000ms</code>, so the function call is ignored.</p>
</li>
</ul>
</li>
<li><p><strong>Third Function Call (Executed After Timeout):</strong></p>
<ul>
<li><p><code>setTimeout</code> schedules <code>sendChatMessageWithSlowMode('Hello again')</code> after 3000ms.</p>
</li>
<li><p><code>now - lastCall</code> is now greater than <code>2000ms</code>, so:</p>
<ul>
<li><p><code>lastCall</code> is updated.</p>
</li>
<li><p><code>sendChatMessage('Hello again')</code> executes successfully.</p>
</li>
</ul>
</li>
</ul>
</li>
</ol>
<h3 id="heading-key-learnings"><strong>Key Learnings</strong></h3>
<ul>
<li><p>The <code>throttle</code> function limits function execution within a specified time frame.</p>
</li>
<li><p>It is useful for optimizing event-heavy scenarios like scrolling, resizing, and API requests.</p>
</li>
<li><p>Throttling ensures that function calls are controlled, preventing unnecessary execution and improving performance.</p>
</li>
</ul>
<h2 id="heading-when-to-use-throttling">When to Use Throttling?</h2>
<p>Use throttling when:</p>
<ul>
<li><p>You want to improve UI performance (e.g., limiting button clicks).</p>
</li>
<li><p>Handling high-frequency events like scrolling or resizing.</p>
</li>
<li><p>Preventing excessive API calls to reduce server load.</p>
</li>
</ul>
<p>Throttling is a powerful technique that helps developers create efficient and smooth web applications by controlling function execution frequency.</p>
<p><strong>Thanks to Piyush Garg for the Insights</strong></p>
<div class="embed-wrapper"><div class="embed-loading"><div class="loadingRow"></div><div class="loadingRow"></div></div><a class="embed-card" href="https://www.youtube.com/watch?v=3o47TTtF2u0">https://www.youtube.com/watch?v=3o47TTtF2u0</a></div>
<p> </p>
<h3 id="heading-connect-to-me-for-more-info">Connect to me for more info…</h3>
<p><a target="_blank" href="https://www.linkedin.com/in/shahbaz-alam-2aa154215/">LinkedIn</a></p>
]]></content:encoded></item></channel></rss>