<?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[deboubcing-and-throttling-in-js]]></title><description><![CDATA[deboubcing-and-throttling-in-js]]></description><link>https://deboubcing-and-throttling-in-js.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Fri, 26 Jun 2026 16:34:02 GMT</lastBuildDate><atom:link href="https://deboubcing-and-throttling-in-js.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Debouncing vs Throttling in JavaScript]]></title><description><![CDATA[When it comes to handling frequent user events like scroll, resize, or input, two powerful techniques stand out: Debouncing and Throttling. Mastering them can greatly improve your app’s performance and user experience.

What is Debouncing?
Definition...]]></description><link>https://deboubcing-and-throttling-in-js.hashnode.dev/debouncing-vs-throttling-in-javascript</link><guid isPermaLink="true">https://deboubcing-and-throttling-in-js.hashnode.dev/debouncing-vs-throttling-in-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[webdevelopment]]></category><category><![CDATA[Debouncing and Throttling]]></category><dc:creator><![CDATA[Deepthi Purijala]]></dc:creator><pubDate>Sat, 28 Jun 2025 06:13:41 GMT</pubDate><content:encoded><![CDATA[<hr />
<p>When it comes to handling frequent user events like <code>scroll</code>, <code>resize</code>, or <code>input</code>, two powerful techniques stand out: <strong>Debouncing</strong> and <strong>Throttling</strong>. Mastering them can greatly improve your app’s performance and user experience.</p>
<hr />
<h2 id="heading-what-is-debouncing">What is <strong>Debouncing</strong>?</h2>
<h3 id="heading-definition">Definition:</h3>
<p>Debouncing delays the execution of a function until a certain amount of time has passed <strong>since the last time</strong> the event was triggered.</p>
<h3 id="heading-real-life-analogy">Real-Life Analogy:</h3>
<blockquote>
<p>Imagine you're typing in a search box. You want to wait until the user stops typing before sending an API call. That's debouncing.</p>
</blockquote>
<h3 id="heading-how-it-works">How It Works:</h3>
<ul>
<li><p>The function waits for a pause.</p>
</li>
<li><p>Each time the event fires, it resets the timer.</p>
</li>
<li><p>Executes only once the event stops for the defined delay.</p>
</li>
</ul>
<h3 id="heading-example-debounced-search-box">Example: Debounced Search Box</h3>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> counter = <span class="hljs-number">0</span>;

<span class="hljs-keyword">const</span> getData = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Fetching data from API"</span>, counter++);
};

<span class="hljs-keyword">const</span> debounce = <span class="hljs-function">(<span class="hljs-params">fn, delay</span>) =&gt;</span> {
  <span class="hljs-keyword">let</span> timer;
  <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-built_in">clearTimeout</span>(timer);
    timer = <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> {
      fn.apply(<span class="hljs-built_in">this</span>, args);
    }, delay);
  };
};

<span class="hljs-keyword">const</span> optimizedFunction = debounce(getData, <span class="hljs-number">1000</span>);

<span class="hljs-built_in">document</span>.getElementById(<span class="hljs-string">"search"</span>).addEventListener(<span class="hljs-string">"input"</span>, optimizedFunction);
</code></pre>
<h3 id="heading-use-cases">Use Cases:</h3>
<ul>
<li><p>Input fields</p>
</li>
<li><p>Form validation</p>
</li>
<li><p>Auto-save</p>
</li>
<li><p>Window resizing</p>
</li>
</ul>
<hr />
<h2 id="heading-what-is-throttling">What is <strong>Throttling</strong>?</h2>
<h3 id="heading-definition-1">Definition:</h3>
<p>Throttling ensures a function is called <strong>only once in a specified time interval</strong>, no matter how many times the event is triggered.</p>
<h3 id="heading-real-life-analogy-1">Real-Life Analogy:</h3>
<blockquote>
<p>Tracking scroll position every second instead of on every pixel change. That’s throttling.</p>
</blockquote>
<h3 id="heading-how-it-works-1">How It Works:</h3>
<ul>
<li><p>The function executes immediately.</p>
</li>
<li><p>Then it <strong>waits</strong> for a fixed period before being allowed to run again.</p>
</li>
</ul>
<h3 id="heading-example-throttled-resize-event">Example: Throttled Resize Event</h3>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> throttle = <span class="hljs-function">(<span class="hljs-params">fn, limit</span>) =&gt;</span> {
  <span class="hljs-keyword">let</span> inThrottle;
  <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">if</span> (!inThrottle) {
      fn.apply(<span class="hljs-built_in">this</span>, args);
      inThrottle = <span class="hljs-literal">true</span>;
      <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> (inThrottle = <span class="hljs-literal">false</span>), limit);
    }
  };
};

<span class="hljs-keyword">let</span> count = <span class="hljs-number">0</span>;

<span class="hljs-keyword">const</span> handleResize = <span class="hljs-function">() =&gt;</span> {
  <span class="hljs-built_in">console</span>.log(<span class="hljs-string">"Window resized"</span>, count++);
};

<span class="hljs-built_in">window</span>.addEventListener(<span class="hljs-string">"resize"</span>, throttle(handleResize, <span class="hljs-number">1000</span>));
</code></pre>
<h3 id="heading-use-cases-1">Use Cases:</h3>
<ul>
<li><p>Scroll listeners</p>
</li>
<li><p>Resize events</p>
</li>
<li><p>Limiting rapid button clicks</p>
</li>
<li><p>API polling</p>
</li>
</ul>
<hr />
<h2 id="heading-key-differences">Key Differences</h2>
<div class="hn-table">
<table>
<thead>
<tr>
<td>Feature</td><td><strong>Debouncing</strong></td><td><strong>Throttling</strong></td></tr>
</thead>
<tbody>
<tr>
<td>Execution Timing</td><td>After a delay</td><td>At regular intervals</td></tr>
<tr>
<td>Function Calls</td><td>Once after pause</td><td>Once per interval</td></tr>
<tr>
<td>Common Use</td><td>Search input</td><td>Scroll/resize</td></tr>
<tr>
<td>Reset Timer</td><td>Yes</td><td>No</td></tr>
</tbody>
</table>
</div><hr />
<h2 id="heading-visual-recap">Visual Recap</h2>
<h3 id="heading-debouncing">Debouncing:</h3>
<pre><code class="lang-javascript">Typing:      a---b---c------(pause)
<span class="hljs-attr">Execution</span>:                 [GET DATA]
</code></pre>
<h3 id="heading-throttling">Throttling:</h3>
<pre><code class="lang-javascript">Scrolling:  --|--|--|--|--|--|--|--|--|--|
Execution:   [ ]   [ ]   [ ]   [ ]   [ ]
</code></pre>
<hr />
<h2 id="heading-conclusion">Conclusion:</h2>
<ul>
<li><p>Use <strong>debouncing</strong> when you want to wait until an action <strong>stops</strong> (e.g., search input).</p>
</li>
<li><p>Use <strong>throttling</strong> when you want to limit an action to happen at <strong>intervals</strong> (e.g., scroll).</p>
</li>
<li><p>Both are essential for <strong>performance optimization</strong>.</p>
</li>
</ul>
<hr />
<blockquote>
<p><strong>Thanks for reading!</strong> If you found this helpful, feel free to share it with fellow developers or bookmark it for later reference.💡</p>
</blockquote>
]]></content:encoded></item></channel></rss>