<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Afraz Momin</title>
    <description>The latest articles on DEV Community by Afraz Momin (@afrazchelsea).</description>
    <link>https://dev.to/afrazchelsea</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F353644%2F9088d048-88c0-4b07-9fa2-37ec3405795a.jpg</url>
      <title>DEV Community: Afraz Momin</title>
      <link>https://dev.to/afrazchelsea</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/afrazchelsea"/>
    <language>en</language>
    <item>
      <title>Debouncing v/s Throttling: What's the difference?</title>
      <dc:creator>Afraz Momin</dc:creator>
      <pubDate>Tue, 29 Sep 2020 09:12:25 +0000</pubDate>
      <link>https://dev.to/afrazchelsea/debouncing-v-s-throttling-what-s-the-difference-4k95</link>
      <guid>https://dev.to/afrazchelsea/debouncing-v-s-throttling-what-s-the-difference-4k95</guid>
      <description>&lt;p&gt;Website performance plays a huge role in enhancing the user experience of our websites. In this article, we will learn about performance optimization techniques like Debouncing and Throttling and the key difference between them.&lt;/p&gt;

&lt;p&gt;Debouncing and Throttling are widely-used techniques that help us in limiting the rate at which a function fires off. These two techniques give us a layer of control between the event and the execution of the functions attached to them. API servers often implement either of these two techniques to prevent the application from being overloaded. &lt;/p&gt;

&lt;p&gt;These function calls could be anything from a simple scroll event to an API call to the server. Both these techniques are almost identical and help us reduce the number of function calls being made but they have one small, but significant difference among them. &lt;/p&gt;

&lt;p&gt;Before we get into the difference, let's understand how they work individually -&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Debouncing?
&lt;/h3&gt;

&lt;p&gt;Debouncing is a technique in which no matter how many times a user fires an event, &lt;br&gt;
the call will be made only after a specific amount of time has passed &lt;strong&gt;after the user stops firing the event.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For example, let's say a user is typing something in a search box. This search box makes API calls and has a debounce function attached to it with a specified time duration of 400ms. So now, unless 400ms have passed after the user stopped typing, the API call wouldn't be made. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7dlrrrimhjh51yx64ue1.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F7dlrrrimhjh51yx64ue1.gif" alt="debouncing.gif" width="800" height="483"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I wrote a detailed article on Debouncing in Javascript, a couple of months ago. If the concept of debouncing is completely new to you, I strongly suggest you &lt;a href="https://blog.afrazmomin.com/debouncing-in-javascript"&gt;go to this link&lt;/a&gt; and read the post before moving ahead with this one.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Throttling?
&lt;/h3&gt;

&lt;p&gt;Throttling is a technique that makes the next function call strictly after a certain period of time. No matter how many times the user fires an event, the function attached will be &lt;strong&gt;executed only once in the given time period.&lt;/strong&gt; &lt;/p&gt;

&lt;h4&gt;
  
  
  Let's understand this by coding a simple throttle function ourselves -
&lt;/h4&gt;

&lt;p&gt;We will start by taking a simple Button. Let's say this button calls some API. The &lt;code&gt;onclick&lt;/code&gt; attribute on this button will call two functions - &lt;code&gt;normalFunc()&lt;/code&gt; and &lt;code&gt;apiWithThrottle()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fn8jrd8nj4iy8t61elqtd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fn8jrd8nj4iy8t61elqtd.png" alt="button-html" width="800" height="202"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In our Javascript file, we'll define the functions -&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2whlzeuysdwz9bph15oj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F2whlzeuysdwz9bph15oj.png" alt="js-code" width="800" height="787"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;normalFunc()&lt;/code&gt; keeps track of the number of clicks made on the button, and &lt;code&gt;apiCallFunc()&lt;/code&gt; keeps track of the number of API calls being made. The function &lt;code&gt;apiWithThrottle()&lt;/code&gt; when triggered by the button, will call the &lt;code&gt;throttle()&lt;/code&gt; function in which the function to be throttled and the time limit are given as parameters.&lt;/p&gt;

&lt;h4&gt;
  
  
  After running this code, we see something like this -
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmj7gsx23z4w9l5r70e2c.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fmj7gsx23z4w9l5r70e2c.gif" alt="throttling.gif" width="753" height="644"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here, we have set the time limit to 1 second (1000ms). Notice how the user clicks the button multiple times but the call to the API is only made 3 times, each after an interval of 1 second. To sum it up in simple words - even if the user clicks the button 15 times in 3 seconds, the number of times the API call will be made is 3 only. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Here's the link to the &lt;a href="https://codepen.io/afrazchelsea/pen/ZEWZZzP?__cf_chl_jschl_tk__=0179b83d2f4126274a036af65c94852eb166cd86-1601352909-0-AXFNLTphxkHBsmCGyeIIV4X53_DUqUBsuUqwuDJo2hEXdw_PljihEnXNP-F7czffsK8uEmpyESR3J4i3DaiMUK-Ud_I3r0fZV8SN4K91Wq6E1_dyGaKA-BQrU2MeI_wl_SubktUz2xPIf-BV7SQqXSLI9mBgRQYmkff0xGVEor8jZYWXV4xxMImy6zjOgkMAK5vMgxE63fpldBX5DUbfrgJNKOe1qRIotDd9GL2g-vX5ooy4ztqTEombA1an1Hg7WEy6gCWAJN9wOm2YzsWmeROuN_aYeC7KzZ1iA7KG_vi-uDY2_onbDJIgvjazYnyDuD3MExodeSq6AYUWeQcH6E8"&gt;CodePen&lt;/a&gt;, if you want to try this out yourself.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Debouncing vs Throttling
&lt;/h3&gt;

&lt;p&gt;The difference between the two can be understood by taking a simple real-life example - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Debouncing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Imagine you're a 7-year toddler who loves chocolates. You persistently keep asking your mom for some chocolates. She gives you some but then you start asking for some more. You ask her so many times that she gets annoyed and tells you that you can have it only if you don't bother her and remain silent for the next one hour. This means if you keep asking her, you will only get it one hour after the last time you asked her. &lt;/p&gt;

&lt;p&gt;This is debouncing.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Throttling&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Consider the same example - You ask your mom for chocolates despite having them a few minutes ago. You persistently keep asking her, she gets annoyed and finally decides to give you some. But she, being your mom, knows that you will ask for some more in a few minutes. So she gives you the chocolates with a condition that you won't get any more for the next one hour. You still keep bothering her for more but now she ignores you. Finally, after an hour has passed, she gives you the chocolates. If you ask for more, you will get it only after an hour, no matter how many times you ask her.&lt;/p&gt;

&lt;p&gt;This is what throttling is!&lt;/p&gt;

&lt;h4&gt;
  
  
  Use-cases
&lt;/h4&gt;

&lt;p&gt;Both these techniques have their own set of use-cases. &lt;/p&gt;

&lt;p&gt;Debouncing can be used when the result of the most recent event occurrence is what is important. For example, a search query on an e-commerce website. &lt;/p&gt;

&lt;p&gt;Throttling can be used when the input provided to the function call doesn't matter or is the same each time. For example, infinite scrolling on a webpage. Here, we need to check how far the user is from the bottom of the page. If they're too close, we request more data and append it to the page. Here debouncing wouldn't work as it would only trigger the event when the user has stopped scrolling but we need to start fetching the content before the user reaches the bottom.&lt;/p&gt;

&lt;p&gt;Another example would be a multiplayer fighting game where your character has to punch to defeat its opponent. Throttling can be applied to this punching ability of the character such that it can punch only once per second. Now even if the player gives the command to punch 10 times in 5 seconds, the number of punches thrown would be 5 only.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wrapping Up
&lt;/h3&gt;

&lt;p&gt;Techniques like debouncing and throttling give us control over the execution of events in our websites, helping us reduce the number of high computational tasks that may hamper the performance of our website. They might have different use-cases but the end goal remains the same i.e better performance. So if you're a developer looking to optimize your website, you know what to do!&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;If you liked what you read, share the article with a friend and connect with me on Twitter - &lt;a href="https://twitter.com/afraz_momin"&gt;@afraz_momin&lt;/a&gt;. I plan on writing similar articles on Javascript in the coming days!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>100daysofcode</category>
    </item>
    <item>
      <title>Function Currying in JavaScript</title>
      <dc:creator>Afraz Momin</dc:creator>
      <pubDate>Sat, 29 Aug 2020 11:15:04 +0000</pubDate>
      <link>https://dev.to/afrazchelsea/function-currying-in-javascript-3h3g</link>
      <guid>https://dev.to/afrazchelsea/function-currying-in-javascript-3h3g</guid>
      <description>&lt;p&gt;Javascript is a multi-paradigm language that allows you to freely mix and match object-oriented, procedural, and functional paradigms. Recently there has been a growing trend towards functional programming. The functional programming style not only attempts to pass functions as arguments, a.k.a callbacks, but also returns back functions as well. This feature of functional programming gives us many new and useful concepts. One of these concepts is Currying.&lt;/p&gt;

&lt;p&gt;In this article, we will take a look at what currying is and how it helps us make our code clean and much simpler.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Currying?
&lt;/h2&gt;

&lt;p&gt;Before diving into currying, we first need to understand the &lt;em&gt;arity&lt;/em&gt; of a function. The &lt;em&gt;arity&lt;/em&gt; of a function is basically the number of arguments it requires. &lt;/p&gt;

&lt;p&gt;Consider these two examples -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;addTwo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;addThree&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, the arity of the function &lt;code&gt;addTwo&lt;/code&gt; is 2 and the arity of the function &lt;code&gt;addThree&lt;/code&gt; is 3. &lt;/p&gt;

&lt;p&gt;Currying a function means to convert a function of N arity into N different functions of arity 1.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In much simpler words, currying is the process of restructuring the function so that it takes only one argument and then returns another function that takes the next argument, and so on.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To give you a sense of how this could work, let's create a couple of functions -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Un-curried function&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Curried function&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;addCurried&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// Returns 3&lt;/span&gt;
&lt;span class="nf"&gt;addCurried&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// Returns 3&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice the difference between the two. The first function is a simple function that takes two parameters -  &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt;, and adds them. On the other hand, the second function &lt;code&gt;addCurried&lt;/code&gt; takes only one argument i.e &lt;code&gt;a&lt;/code&gt;, and returns another function which takes &lt;code&gt;b&lt;/code&gt; as the argument. Both give us the same result.&lt;/p&gt;

&lt;p&gt;Currying can be helpful when we can't provide all arguments to a function at one time.&lt;/p&gt;

&lt;p&gt;Each function call can be saved into a variable, which will hold the returned function reference that takes the next argument when it's available. Let's take another example to understand this better -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Curried function&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;english&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;spanish&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hola,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;german&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Geuten Tag,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;german&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Dwight&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Prints "Geuten Tag, Dwight"&lt;/span&gt;

&lt;span class="nf"&gt;spanish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Oscar&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Prints "Hola, Oscar"&lt;/span&gt;

&lt;span class="nf"&gt;english&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Michael&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Prints "Hello, Michael"&lt;/span&gt;
&lt;span class="nf"&gt;english&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Jim&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Prints "Hello, Jim"&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice how currying helps us avoid passing the same variable again and again. In this case, the variable &lt;code&gt;msg&lt;/code&gt;, which will print  "Hello" when called using &lt;code&gt;english&lt;/code&gt;. This is a great strategy to avoid frequently calling a function with the same argument.&lt;/p&gt;

&lt;h3&gt;
  
  
  ES6 Pattern
&lt;/h3&gt;

&lt;p&gt;Currying is part of functional programming and such curried functions can also be easily written using the arrow function syntax in ES6 for more cleaner and elegant code in the following way :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;addCurried&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="nf"&gt;addCurried&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;   &lt;span class="c1"&gt;// Returns 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Partial Application Function
&lt;/h3&gt;

&lt;p&gt;When we talk about currying, the concept of Partial Application Function is bound to come up. Currying and Partial application function are almost identical concepts with one significant difference.&lt;/p&gt;

&lt;p&gt;Partial application function is a function that returns another function but each returned function can take multiple arguments. In currying, the returning function can only take one argument.&lt;/p&gt;

&lt;p&gt;Let's take a simple example to demonstrate this difference-&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Curried function&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;addCurried&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Partial Application function&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;addPartial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;y&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;z&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;addCurried&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Returns 6&lt;/span&gt;
&lt;span class="nf"&gt;addPartial&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Returns 6&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The implementation of both these concepts totally depends upon the use-case and the availability of the arguments at that point in time. But they definitely help us write more cleaner and elegant code.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wrapping Up
&lt;/h3&gt;

&lt;p&gt;Currying is an incredibly useful concept when it comes to functional programming with Javascript. As seen, using currying we can have functions that are more definite in what they do, avoiding potential repetition in our code, which ultimately leads to simplification of our code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If you liked what you read, connect with me on &lt;a href="https://twitter.com/afraz_momin"&gt;Twitter - @afraz_momin&lt;/a&gt;.&lt;br&gt;
I plan to write similar articles on JavaScript in the coming days!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>100daysofcode</category>
    </item>
    <item>
      <title>Event Bubbling and Event Capturing in Javascript</title>
      <dc:creator>Afraz Momin</dc:creator>
      <pubDate>Sat, 04 Jul 2020 11:30:12 +0000</pubDate>
      <link>https://dev.to/afrazchelsea/event-bubbling-and-event-capturing-in-javascript-3og8</link>
      <guid>https://dev.to/afrazchelsea/event-bubbling-and-event-capturing-in-javascript-3og8</guid>
      <description>&lt;p&gt;The interactivity of our HTML web page is handled by Javascript. This interactivity is nothing but a bunch of events that the HTML elements undergo. An event can be something the browser does or something a user does. They tell us some change has happened and where it has happened. It could an onClick event that indicates something has been clicked. Another instance could be an onSubmit event that says that the form has been submitted.&lt;/p&gt;

&lt;p&gt;How well these events are handled decide how user-friendly the web page is.&lt;/p&gt;

&lt;p&gt;Event Bubbling and Event Capturing are two phases of event propagation/flow in Javascript. Event flow is basically the order in which the events are received on a web page. In Javascript, event flow takes place in three phases -&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Capture Phase&lt;/li&gt;
&lt;li&gt;Target Phase&lt;/li&gt;
&lt;li&gt;Bubble Phase&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This propagation is bidirectional, from the window to the target and back. What differentiates these phases is the type of listeners that are called.&lt;/p&gt;

&lt;p&gt;Let's start by understanding Bubbling first.&lt;/p&gt;

&lt;h2&gt;
  
  
  Event Bubbling:
&lt;/h2&gt;

&lt;p&gt;Bubbling is the flow of events where, when an event takes place on an element, it first runs the handler on itself, then on its parent and then on all of its ancestors.&lt;/p&gt;

&lt;p&gt;It basically moves up the hierarchy from the innermost element to the outermost element.&lt;/p&gt;

&lt;p&gt;This can be better understood by taking an example -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;body&amp;gt;
    &amp;lt;div id="grandparent"&amp;gt;
      &amp;lt;p&amp;gt;Grandparent&amp;lt;/p&amp;gt;
      &amp;lt;div id="parent"&amp;gt;
        &amp;lt;p&amp;gt;Parent&amp;lt;/p&amp;gt;
        &amp;lt;div id="child"&amp;gt;
          &amp;lt;p&amp;gt;Child&amp;lt;/p&amp;gt;
        &amp;lt;/div&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
    &amp;lt;button onClick="history.go(0)"&amp;gt;
      Reset Elements
    &amp;lt;/button&amp;gt;
  &amp;lt;/body&amp;gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our HTML file, we take 3 divs nested one inside the other and give them ids of &lt;code&gt;child&lt;/code&gt;, &lt;code&gt;parent&lt;/code&gt;, and &lt;code&gt;grandparent&lt;/code&gt; starting from the innermost div.&lt;/p&gt;

&lt;h4&gt;
  
  
  Add a bit of styling
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div {
  min-width: 75px;
  min-height: 75px;
  padding: 25px;
  border: 1px solid black;
}

button {
  margin-top: 20px;
  width: 200px;
  font-size: 14px;
  padding: 10px;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will set a &lt;code&gt;click&lt;/code&gt; event on each of the 3 divs in our JS file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.querySelector("#grandparent").addEventListener("click", () =&amp;gt; {
  document.querySelector("#grandparent &amp;gt; p").textContent =
    "Grandparent Clicked!";
  console.log("Grandparent Clicked");
});

document.querySelector("#parent").addEventListener("click", () =&amp;gt; {
  document.querySelector("#parent &amp;gt; p").textContent = "Parent Clicked!";
  console.log("Parent Clicked");
});

document.querySelector("#child").addEventListener("click", () =&amp;gt; {
  document.querySelector("#child &amp;gt; p").textContent = "Child Clicked!";
  console.log("Child Clicked");
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  The code above will perform in the following way -
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4B-MECzr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1593269603296/jIPWMOyme.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4B-MECzr--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1593269603296/jIPWMOyme.gif" alt="first.gif" width="800" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice how, even when the &lt;code&gt;child&lt;/code&gt; div is clicked, the handlers on all its ancestors get triggered as well. Similarly, when the &lt;code&gt;parent&lt;/code&gt; div is clicked, the handler on the &lt;code&gt;grandparent&lt;/code&gt; div will get fired as well. But, keep in mind, in this example, the handler on the &lt;code&gt;child&lt;/code&gt; div won't be triggered.&lt;/p&gt;

&lt;p&gt;Although, what's more important here is the way the event flow happened. It started from the innermost element, i.e. is the &lt;code&gt;child&lt;/code&gt; div and then propagated up the hierarchy eventually reaching the &lt;code&gt;parent&lt;/code&gt; and &lt;code&gt;grandparent&lt;/code&gt; divs (in that order strictly).&lt;/p&gt;

&lt;p&gt;This type of flow of events is called Event Bubbling.&lt;/p&gt;

&lt;h2&gt;
  
  
  Event Capturing:
&lt;/h2&gt;

&lt;p&gt;The capturing principle is the exact opposite of bubbling.&lt;br&gt;
In Event Capturing, the event propagation takes place from the outermost element to the innermost element. Event capturing is sometimes also referred to as &lt;strong&gt;event trickling&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We often use the &lt;code&gt;addEventListener()&lt;/code&gt; when working with Javascript, in which we usually pass two parameters -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;the event&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;the callback function&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;addEventListener()&lt;/code&gt; function also takes a 3rd hidden parameter - &lt;code&gt;useCapture&lt;/code&gt; which takes a boolean value. This &lt;code&gt;useCapture&lt;/code&gt; parameter is set to default by false. Setting it to false, makes our events propagate using the principle of Bubbling. Setting it to true will make them propagate in a top-down approach, that is, Capturing.&lt;/p&gt;

&lt;p&gt;To implement event capturing we will make a few small changes to our JS code -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;document.querySelector("#grandparent").addEventListener("click", () =&amp;gt; {
  document.querySelector("#grandparent &amp;gt; p").textContent =
    "Grandparent Clicked!";
  console.log("Grandparent Clicked");
},true); // useCapture parameter is now set to true

document.querySelector("#parent").addEventListener("click", () =&amp;gt; {
  document.querySelector("#parent &amp;gt; p").textContent = "Parent Clicked!";
  console.log("Parent Clicked");
},true); // useCapture parameter is now set to true

document.querySelector("#child").addEventListener("click", () =&amp;gt; {
  document.querySelector("#child &amp;gt; p").textContent = "Child Clicked!";
  console.log("Child Clicked");
},true); // useCapture parameter is now set to true

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now our code will run in the following way -&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--HItf0Vao--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1593269619661/0n9fcRgPd.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--HItf0Vao--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1593269619661/0n9fcRgPd.gif" alt="second.gif" width="800" height="495"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice how the flow of events now propagates from the outermost element to the innermost element.&lt;/p&gt;

&lt;p&gt;i.e &lt;code&gt;grandparent&lt;/code&gt; -&amp;gt; &lt;code&gt;parent&lt;/code&gt; -&amp;gt; &lt;code&gt;child&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This flow of events is called Event Capturing.&lt;/p&gt;

&lt;h4&gt;
  
  
  Wrapping Up
&lt;/h4&gt;

&lt;p&gt;The reason I spoke about bubbling first is because event capturing is rarely used. It is set to false by default. For most of the browsers, Event bubbling is the default way of event flow.&lt;/p&gt;

&lt;p&gt;Javascript helps us make interactive web applications. It makes use of a lot of user-generated events in doing so. The user experience of a website depends on how well these events are handled. Hence it is important to know how events work and the flow behind them.&lt;/p&gt;

&lt;p&gt;Here's the link to the &lt;a href="https://codepen.io/afrazchelsea/pen/abdpqgy?editors=1000"&gt;Codepen&lt;/a&gt;, if you want to demonstrate this yourself.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If you liked what you read, follow me on Twitter - &lt;a href="https://twitter.com/afraz_momin"&gt;@afraz_momin&lt;/a&gt; to stay updated.&lt;br&gt;
I plan on writing similar articles about JavaScript in the coming days!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
      <category>100daysofcode</category>
    </item>
    <item>
      <title>Debouncing in JavaScript</title>
      <dc:creator>Afraz Momin</dc:creator>
      <pubDate>Tue, 23 Jun 2020 15:37:13 +0000</pubDate>
      <link>https://dev.to/afrazchelsea/debouncing-in-javascript-36n8</link>
      <guid>https://dev.to/afrazchelsea/debouncing-in-javascript-36n8</guid>
      <description>&lt;p&gt;Often situations arise, when we want a certain event to fire off only after a certain amount of time has passed. The most common example would be the search bars we see on e-commerce websites like Amazon or Flipkart. In these search bars, when you type something, an API call is made, and results similar to your query are displayed on the screen.&lt;/p&gt;

&lt;p&gt;Now imagine making an API call every time a key is pressed on your keyboard. Doesn't sound viable right? Firing an event continuously (in this case, the call to the API) would hamper the performance of the website.&lt;/p&gt;

&lt;p&gt;This can be avoided by using the concept of debouncing. Debouncing limits the rate at which a function fires off. In the example above, it drastically reduces the number of API calls that are made to the server.&lt;/p&gt;

&lt;p&gt;The setTimeout function of Javascript plays a vital role in implementing debouncing. For those who don't know what setTimeout does, it is a scheduling function provided by Javascript which allows us to run a task after a certain interval of time.&lt;/p&gt;

&lt;h4&gt;
  
  
  Let's see how we can implement debouncing with a simple example,
&lt;/h4&gt;

&lt;p&gt;The very first thing we'll do here is - create a simple input in our HTML file. On this input box, we will call a method &lt;code&gt;searchData()&lt;/code&gt; for the &lt;code&gt;onkeyup&lt;/code&gt; event. So that every time the user releases the key after pressing it, &lt;code&gt;searchData()&lt;/code&gt; is called.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;body&amp;gt;
    &amp;lt;label for="search"&amp;gt;Search&amp;lt;/label&amp;gt;&amp;lt;br /&amp;gt;&amp;lt;br /&amp;gt;
    &amp;lt;input type="text" onkeyup="searchData()" /&amp;gt;

    &amp;lt;script src="script.js"&amp;gt;&amp;lt;/script&amp;gt;
  &amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our Javascript file, we'll define the &lt;code&gt;searchData()&lt;/code&gt; function and create a counter to track the number of times it gets called.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let counter = 1; // To track number of calls

const searchData = () =&amp;gt; {
  // Let's say this function calls some API and gets us some data
  console.log("Looking for data..", counter++);
};

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  After running this program, we see something like this -
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--4Mbg_sfc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1592509132273/zPQonjL7A.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--4Mbg_sfc--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1592509132273/zPQonjL7A.gif" alt="first.gif" width="800" height="483"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice how for every letter of the word "Michael", the function gets called. Imagine firing off API calls for a bigger query on a much bigger site. That would be very browser intensive and may hinder the performance. To reduce these continuous function calls, we'll make use of the debounce function :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let counter = 1; // To track number of calls

const searchData = () =&amp;gt; {
  // Let's say this function calls some API and gets us some data
  console.log("Looking for data..", counter++);
};

// Debounce function
const debounce = function (fn, delay) {
  let timer = 0;
  return function () {
    clearTimeout(timer);
    timer = setTimeout(() =&amp;gt; {
      fn();
    }, delay);
  };
};

const callDebounce = debounce(searchData, 300);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that we make a change in our HTML file and will be calling &lt;code&gt;callDebounce()&lt;/code&gt; now instead of &lt;code&gt;searchData()&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; &amp;lt;input type="text" onkeyup="callDebounce()" /&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  The code above will do the following -
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--oeydhQN6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1592511364053/fNxHJIJGF.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--oeydhQN6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_800/https://cdn.hashnode.com/res/hashnode/image/upload/v1592511364053/fNxHJIJGF.gif" alt="second-final.gif" width="800" height="483"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Notice how no call is made until a delay of 300ms is encountered. Instead of making more than a dozen calls for each letter in "Michael Scott", our input bar will only make calls whenever the delay between two keypresses is more than 300 milliseconds. In this case, 2 times.&lt;/p&gt;

&lt;p&gt;When the user enters the first character "M", &lt;code&gt;debounce()&lt;/code&gt; is triggered which waits for 300ms to fire off the &lt;code&gt;searchData()&lt;/code&gt; function. But before 300ms have passed, the user enters another character which will wait another 300ms to fire off &lt;code&gt;searchData()&lt;/code&gt; and so on. This will create multiple copies of the timer running in the background. So to clear out the timers in cases where consecutive keypresses are being made without a pause 300ms, we make use of the inbuilt &lt;code&gt;clearTimeout()&lt;/code&gt; function. This sets the &lt;code&gt;timer&lt;/code&gt; to 0.&lt;/p&gt;

&lt;p&gt;This goes on until the user takes a pause after completing the word "Michael", this pause is noticed by the function which in turn fires off our &lt;code&gt;searchData()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;In simple words, what happens here is that the function waits 300 milliseconds after the last keypress. Only when 300ms have passed and no keypress has been made, it fires off the &lt;code&gt;searchData()&lt;/code&gt; function.&lt;/p&gt;

&lt;h3&gt;
  
  
  Wrapping Up
&lt;/h3&gt;

&lt;p&gt;This was a simple demonstration of debouncing. Using this we can reduce the number of calls made several times in succession by a function, which will ultimately lead to an improvement in the performance of our website.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This article was originally posted &lt;a href="https://afrazmomin.hashnode.dev/debouncing-in-javascript-ckbq548q40099lgs1lvs7opks"&gt;here&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If you liked what you read, consider following me on Twitter - &lt;a href="https://twitter.com/afraz_momin"&gt;@afraz_momin&lt;/a&gt; to stay updated. I plan on writing similar articles about JavaScript in the coming days!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>React v/s Vanilla JS - When to use what?</title>
      <dc:creator>Afraz Momin</dc:creator>
      <pubDate>Tue, 14 Apr 2020 14:32:04 +0000</pubDate>
      <link>https://dev.to/afrazchelsea/react-vs-vanilla-js-what-why-and-when-1jin</link>
      <guid>https://dev.to/afrazchelsea/react-vs-vanilla-js-what-why-and-when-1jin</guid>
      <description>&lt;p&gt;Web apps can be complex and may require a lot of dynamic functionalities. One can opt for Vanilla JS to build their applications but if you have worked with Vanilla JS before, you know how messy it can get. Here's when JS frameworks like, React, Angular and Vue, come into the picture.&lt;/p&gt;

&lt;p&gt;In this article, I'll walk you through the main differences between a JS library like React and plain Javascript - when to choose which and why?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ff3n9ae95mmihwosqvk25.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Ff3n9ae95mmihwosqvk25.jpeg" alt="meme" width="756" height="756"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's start by answering two simple questions.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Vanilla JS?
&lt;/h3&gt;

&lt;p&gt;Vanilla JS is nothing but plain JS without any external libraries or frameworks. Using this we can build powerful and cross-platform applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is React?
&lt;/h3&gt;

&lt;p&gt;React is a Javascript library used for building user interfaces. It allows us to make complex UIs from isolated pieces of code called "components".&lt;/p&gt;

&lt;p&gt;React has quickly become one of the most popular Javascript libraries. This is entirely because of its flexibility and the improvement it brings in the performance. React breaks down the UI into smaller and reusable components that can move around data amongst each other. This breaking down of the UI is what gives React an edge over Vanilla JS.&lt;/p&gt;

&lt;p&gt;In Vanilla JS, the code becomes very difficult to maintain if the application is large because in such cases the UI needs to be updated regularly. Here, to change a UI element you need to first find the element in the DOM and then update it. This is fine when you have to update only a single element but imagine doing this on long-form which a user needs to fill. This could be very memory and browser intensive.&lt;/p&gt;

&lt;p&gt;This is where React comes in with a great feature i.e its own virtual DOM. The virtual DOM is a shortcut to bypass the manual work. It is a lightweight copy of the actual DOM. It has the same properties as the real DOM but lacks the power to make changes on the screen.&lt;/p&gt;

&lt;p&gt;The most important and fundamental reason why modern frameworks are used is that, &lt;strong&gt;with Vanilla JS, keeping the UI in sync with the state is hard&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Let's understand this by taking an example
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Consider Facebook. Say at a given time, your friends comment on a picture of yours and you want to see it immediately. You'd want to shift from liking posts to commenting or sharing without being slowed down.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now, this can be done in Vanilla JS too, but the number of changes you'd have to do in the code would be very tedious. All this tiresome work can be avoided by using something like React.&lt;/p&gt;

&lt;p&gt;Basically, with React, we can manage to keep &lt;strong&gt;the UI and the state synchronized with each other&lt;/strong&gt;. In Vanilla JS, if you have to change the state, you would need to update the UI. One small mistake and your UI could be out of sync with your data.&lt;/p&gt;

&lt;p&gt;Code organization and re-use is another important aspect of React. A component created only once can be used multiple times with different data.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Whether you should use Vanilla JS or React depends very much on your use case.&lt;/p&gt;

&lt;p&gt;Vanilla JS is awesome but it's not a great alternative when it comes to building huge applications with complex dynamic functionalities. Besides, it cannot create complex and efficient UIs. So if you have an app that changes frequently and drastically with thousands of pages, it is better to use a modern Javascript framework.&lt;/p&gt;

&lt;p&gt;On the other hand, React which allows us to use reusable components and is capable of keeping the UI in sync with the state, can definitely solve this problem.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If you liked this post&lt;/strong&gt;, you can find more by:&lt;/p&gt;

&lt;p&gt;Following me on twitter: &lt;a href="https://twitter.com/afraz_momin"&gt;@afraz_momin&lt;/a&gt;&lt;br&gt;
Joining the newsletter: &lt;a href="https://afrazblogs.netlify.com/"&gt;afrazblogs.netlify.com&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
