<?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[Sohail's Coding Blog]]></title><description><![CDATA[Sohail's Coding Blog]]></description><link>https://coding.sohail.blog</link><generator>RSS for Node</generator><lastBuildDate>Fri, 10 Apr 2026 13:37:49 GMT</lastBuildDate><atom:link href="https://coding.sohail.blog/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Throttleling in JavaScript]]></title><description><![CDATA[The Throttle function bears resemblance to the Debounce function, albeit with nuanced distinctions in its operational behavior. Rather than constraining the frequency at which a function is invoked, the Throttle function curtails the rate at which th...]]></description><link>https://coding.sohail.blog/throttleling-in-javascript</link><guid isPermaLink="true">https://coding.sohail.blog/throttleling-in-javascript</guid><category><![CDATA[coding]]></category><category><![CDATA[JavaScript]]></category><dc:creator><![CDATA[Sohail Khan]]></dc:creator><pubDate>Fri, 04 Aug 2023 22:45:57 GMT</pubDate><content:encoded><![CDATA[<p>The Throttle function bears resemblance to the Debounce function, albeit with nuanced distinctions in its operational behavior. Rather than constraining the frequency at which a function is invoked, the Throttle function curtails the rate at which the function is executed. Specifically, it proscribes execution if the function has been previously summoned within a designated time frame. This ensures that a particular function is executed at a uniform rate, precluding excessive triggering.</p>
<p>Here is the code snippet that demonstrates the implementation of the Throttle function:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">throttle</span>(<span class="hljs-params">func, delay</span>) </span>{
  <span class="hljs-keyword">let</span> wait = <span class="hljs-literal">false</span>;

  <span class="hljs-keyword">return</span> <span class="hljs-function">(<span class="hljs-params">...args</span>) =&gt;</span> {
    <span class="hljs-keyword">if</span> (wait) {
        <span class="hljs-keyword">return</span>;
    }

    func(...args);
    wait = <span class="hljs-literal">true</span>;
    <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> {
      wait = <span class="hljs-literal">false</span>;
    }, delay);
  }
}
</code></pre>
<p>Within this excerpt, the throttle function will invoke the supplied function <code>func</code>, modify a <code>wait</code> variable to <code>true</code>, and subsequently initiate a timer that will revert the <code>wait</code> parameter after the specified delay has elapsed. If the throttle function is called again during this period, it will either invoke the provided function or simply return if the <code>wait</code> parameter remains <code>true</code>.</p>
<p>The Throttle functionality finds applicability in scenarios such as executing a function to modify the layout of a webpage during scrolling. In the absence of a throttle function, the update function would be invoked repeatedly as the user scrolls through the page, potentially leading to significant performance degradation. By employing the throttle function, execution can be restricted to once every specified number of milliseconds (e.g., X milliseconds), thereby enhancing the webpage's responsiveness and efficiency.</p>
<p>The subsequent snippet illustrates how the throttle function can be utilized:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Define the function responsible for layout modification</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">updateLayout</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">// Implement layout updates...</span>
}

<span class="hljs-comment">// Instantiate a throttled variant of the function</span>
<span class="hljs-keyword">const</span> throttledUpdateLayout = throttle(updateLayout, <span class="hljs-number">250</span>);

<span class="hljs-comment">// Register a listener for window scroll events, invoking the throttled function</span>
<span class="hljs-built_in">window</span>.addEventListener(<span class="hljs-string">"scroll"</span>, throttledUpdateLayout);
</code></pre>
<p>By crafting the <code>throttleUpdatedLayout</code> function and designating a delay of 250 milliseconds, it can be ensured that the <code>updateLayout</code> function will be executed at most once within each 250-millisecond interval during window scrolling. Should the event be triggered prior to the completion of the delay, no action will be taken. This mechanism fosters a more streamlined and responsive user experience, mitigating the risk of overloading the system with redundant function calls.</p>
]]></content:encoded></item><item><title><![CDATA[Debounce in JavaScript]]></title><description><![CDATA[The debounce function represents a sophisticated technique for inhibiting the recurrent activation of a function in response to a rapid sequence of events. This method operates by deferring the execution of a function until a specified interval has e...]]></description><link>https://coding.sohail.blog/debounce-in-javascript</link><guid isPermaLink="true">https://coding.sohail.blog/debounce-in-javascript</guid><category><![CDATA[JavaScript]]></category><category><![CDATA[coding]]></category><dc:creator><![CDATA[Sohail Khan]]></dc:creator><pubDate>Thu, 03 Aug 2023 21:43:18 GMT</pubDate><content:encoded><![CDATA[<p>The debounce function represents a sophisticated technique for inhibiting the recurrent activation of a function in response to a rapid sequence of events. This method operates by deferring the execution of a function until a specified interval has elapsed without any occurrence of the triggering event. As a practical solution, the Debounce function can be leveraged in real-world scenarios to enhance performance by forestalling the execution of functions, particularly when a user engages in frenetic interactions such as incessant button clicking.</p>
<p>Below is a code snippet illustrating the implementation of the debounce function in JavaScript:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">debounce</span>(<span class="hljs-params">func, delay</span>) </span>{
  <span class="hljs-keyword">let</span> timeout;
  <span class="hljs-keyword">return</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">const</span> context = <span class="hljs-built_in">this</span>;
    <span class="hljs-keyword">const</span> args = <span class="hljs-built_in">arguments</span>;
    <span class="hljs-built_in">clearTimeout</span>(timeout);
    timeout = <span class="hljs-built_in">setTimeout</span>(<span class="hljs-function">() =&gt;</span> func.apply(context, args), delay);
  };
}
</code></pre>
<p>In this JavaScript excerpt, the debounce function yields a novel function that invokes the original function subsequent to a predetermined delay. Should the function be invoked anew, the timeout will be annulled, thereby deferring the function's invocation.</p>
<p>This mechanism proves invaluable when dealing with functions that modify the layout of a webpage in response to window resizing. Absent the Debounce function, the layout-modifying function would be summoned repeatedly in rapid succession as the user alters the window's dimensions, potentially leading to performance degradation. By employing the Debounce function, the frequency of layout updates can be judiciously controlled, thereby augmenting the page's responsiveness and efficiency.</p>
<p>The following snippet delineates how the Debounce function can be applied to this specific context:</p>
<pre><code class="lang-javascript"><span class="hljs-comment">// Define the function responsible for layout modification</span>
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">updateLayout</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-comment">// Implement layout updates...</span>
}

<span class="hljs-comment">// Instantiate a debounced variant of the function</span>
<span class="hljs-keyword">const</span> debouncedUpdateLayout = debounce(updateLayout, <span class="hljs-number">250</span>);

<span class="hljs-comment">// Register a listener for window resize events, invoking the debounced function</span>
<span class="hljs-built_in">window</span>.addEventListener(<span class="hljs-string">"resize"</span>, debouncedUpdateLayout);
</code></pre>
<p>In this illustration, the updateLayout function will be summoned at most once within each 250-millisecond interval during window resizing. This functionality guarantees that the layout is refreshed solely 250ms subsequent to the cessation of the user's resizing activity, thereby optimizing the webpage's efficiency and responsiveness.</p>
]]></content:encoded></item><item><title><![CDATA[Bubble Sort in JavaScript]]></title><description><![CDATA[Here I am solving a problem with nested loops

// 1. Create nested loop to compare each element with the next element
// 2. Compare each element with next one and if next element is smaller then previous one, swap them
// 3. Repeat step 2 until the e...]]></description><link>https://coding.sohail.blog/bubble-sort-in-javascript</link><guid isPermaLink="true">https://coding.sohail.blog/bubble-sort-in-javascript</guid><dc:creator><![CDATA[Sohail Khan]]></dc:creator><pubDate>Sat, 16 Jul 2022 22:08:41 GMT</pubDate><content:encoded><![CDATA[<p>Here I am solving a problem with nested loops</p>
<pre><code>
<span class="hljs-comment">// 1. Create nested loop to compare each element with the next element</span>
<span class="hljs-comment">// 2. Compare each element with next one and if next element is smaller then previous one, swap them</span>
<span class="hljs-comment">// 3. Repeat step 2 until the end of loop</span>

const kaySort <span class="hljs-operator">=</span> (arr) <span class="hljs-operator">=</span><span class="hljs-operator">&gt;</span> {
    <span class="hljs-keyword">for</span> (let i <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; i <span class="hljs-operator">&lt;</span> arr.<span class="hljs-built_in">length</span>; i<span class="hljs-operator">+</span><span class="hljs-operator">+</span>)
        {
        <span class="hljs-keyword">for</span> (let j <span class="hljs-operator">=</span> <span class="hljs-number">0</span>; j <span class="hljs-operator">&lt;</span> arr.<span class="hljs-built_in">length</span>; j<span class="hljs-operator">+</span><span class="hljs-operator">+</span>)
            {
                <span class="hljs-keyword">if</span> (arr[j<span class="hljs-operator">+</span><span class="hljs-number">1</span>] <span class="hljs-operator">&lt;</span> arr[j]) {
                    let tmpSwap <span class="hljs-operator">=</span> arr[j];
                    arr[j] <span class="hljs-operator">=</span> arr[j<span class="hljs-operator">+</span><span class="hljs-number">1</span>];
                    arr[j<span class="hljs-operator">+</span><span class="hljs-number">1</span>] <span class="hljs-operator">=</span> tmpSwap;
                }
            }
        }
<span class="hljs-keyword">return</span> arr;
}

console.log(kaySort([<span class="hljs-number">30</span>,<span class="hljs-number">125</span>,<span class="hljs-number">15</span>,<span class="hljs-number">50</span>]));
</code></pre>]]></content:encoded></item></channel></rss>