<?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: K.Saravanakumar</title>
    <description>The latest articles on DEV Community by K.Saravanakumar (@saravanakumarke).</description>
    <link>https://dev.to/saravanakumarke</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%2F685192%2Fe82b6ca8-70c7-42bb-b46f-0fb5cdc75131.jpeg</url>
      <title>DEV Community: K.Saravanakumar</title>
      <link>https://dev.to/saravanakumarke</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/saravanakumarke"/>
    <language>en</language>
    <item>
      <title>Microtasks and (Macro)tasks in Event Loop</title>
      <dc:creator>K.Saravanakumar</dc:creator>
      <pubDate>Sun, 15 Aug 2021 02:01:40 +0000</pubDate>
      <link>https://dev.to/saravanakumarke/microtasks-and-macro-tasks-in-event-loop-4h2h</link>
      <guid>https://dev.to/saravanakumarke/microtasks-and-macro-tasks-in-event-loop-4h2h</guid>
      <description>&lt;p&gt;JavaScript has a concurrency model based on an &lt;strong&gt;event loop&lt;/strong&gt;, which is responsible for executing the code, collecting and processing events, and executing queued sub-tasks.&lt;/p&gt;

&lt;p&gt;Here, we will see about microtask and macrotask in event loop and how event loop will handle tasks.&lt;/p&gt;

&lt;p&gt;let’s dive in! 🏃‍♂️&lt;/p&gt;

&lt;p&gt;Within the Event Loop, there are actually 2 type of queues: the (macro)task queue (or just called the task queue), and the microtask queue. The (macro)task queue is for (macro)tasks and the microtask queue is for microtasks.&lt;/p&gt;

&lt;h3&gt;
  
  
  Microtask
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;microtask&lt;/strong&gt; is a short function which is executed after the function or program which created it exits and only if the &lt;strong&gt;JavaScript execution stack is empty&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Promise callback&lt;/li&gt;
&lt;li&gt;queueMicrotask&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Macrotask
&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;macrotask&lt;/strong&gt; is short function which is executed after &lt;strong&gt;JavaScript execution stack and microtask are empty&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;setTimeout&lt;/li&gt;
&lt;li&gt;setInterval&lt;/li&gt;
&lt;li&gt;setImmediate&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Explanation
&lt;/h3&gt;

&lt;p&gt;When a Promise resolves and calls its then(), catch() or finally(), method, the callback within the method gets added to the microtask queue! This means that the callback within the then(), catch() or finally() method isn’t executed immediately, essentially adding some async behavior to our JavaScript code!&lt;/p&gt;

&lt;p&gt;So when is a then(), catch() or finally() callback executed?🤷‍♂️&lt;/p&gt;

&lt;p&gt;Here the &lt;strong&gt;event loop gives a different priority to the tasks&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;All functions in that are currently in the &lt;strong&gt;call stack get executed&lt;/strong&gt;. When they returned a value, they get &lt;strong&gt;popped&lt;/strong&gt; off the stack.&lt;/p&gt;

&lt;p&gt;When the &lt;strong&gt;call stack is empty&lt;/strong&gt;, all queued up &lt;strong&gt;microtasks are popped&lt;/strong&gt; onto the call stack one by one, and get executed! (Microtasks themselves can also return new microtasks, effectively creating an infinite microtask loop).&lt;/p&gt;

&lt;p&gt;If both the &lt;strong&gt;call stack and microtask queue are empty&lt;/strong&gt;, the event loop checks if there are tasks left on the &lt;strong&gt;(macro)task queue&lt;/strong&gt;. The tasks get popped onto the call stack, executed, and popped off!&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;Task1: A function that’s added to the call stack immediately, for example by invoking it instantly in our code.&lt;/p&gt;

&lt;p&gt;Task2, Task3, Task4: microtasks, for example a promise then callback, or a task added with queueMicrotask.&lt;/p&gt;

&lt;p&gt;Task5, Task6: a (macro)task, for example a setTimeout or setImmediate callback&lt;/p&gt;

&lt;p&gt;First, Task1 returned a value and got popped off the call stack. Then, the engine checked for tasks queued in the microtask queue.&lt;br&gt;
Once all the tasks were put on the call stack and eventually popped off, the engine checked for tasks on the (macro)task queue, which got popped onto the call stack, and popped off when they returned a value.&lt;/p&gt;

&lt;p&gt;Here’s graphic illustration of the event loop 👇&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--KIU-4Ydp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kr6bsfd5o0ipc85kyaf8.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--KIU-4Ydp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kr6bsfd5o0ipc85kyaf8.gif" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Congratulations for reading until the end! In this article you’ve learned:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How microtask and macrotask are working in event loop.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I hope you find this article helpful in understanding microtask and macrotask how it works.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Suggestions are highly appreciated ❤️&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>JavaScript ASYNC &amp; DEFER Attributes</title>
      <dc:creator>K.Saravanakumar</dc:creator>
      <pubDate>Sat, 14 Aug 2021 02:53:04 +0000</pubDate>
      <link>https://dev.to/saravanakumarke/javascript-async-defer-attributes-28k0</link>
      <guid>https://dev.to/saravanakumarke/javascript-async-defer-attributes-28k0</guid>
      <description>&lt;p&gt;Rendering DOM or Webpage faster is one of challenging task that every developer face🤯. One of the main factors of the slow rendering of DOM is the strategy with which we load the JavaScript because JS blocks the parsing of HTML and that’s why the DOM performance highly depends on it.&lt;/p&gt;

&lt;p&gt;However, few keywords can be used with &lt;code&gt;script&lt;/code&gt; tag to boost the performance. The &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; tag has two attributes that do the magic⚡️ for us : &lt;code&gt;async&lt;/code&gt; and &lt;code&gt;defer&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Here’s what we’ll cover in this article:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; without attributes.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; &lt;strong&gt;async&lt;/strong&gt; attributes.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; &lt;strong&gt;defer&lt;/strong&gt; attributes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So let’s dive in!&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; without attributes
&lt;/h3&gt;

&lt;p&gt;As soon as the browser encounters the &lt;code&gt;script&lt;/code&gt; tag, the HTML parsing is blocked and will only start up again after the JavaScript is fetched from the server and executed.&lt;/p&gt;

&lt;p&gt;While the Javascript is loading, the building up of the DOM is paused and that &lt;strong&gt;reduces the performance&lt;/strong&gt; and &lt;strong&gt;increases the loading time of the webpage&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; async attributes
&lt;/h3&gt;

&lt;p&gt;Using &lt;code&gt;async&lt;/code&gt; attribute downloads the script files asynchronously during HTML parsing ( in the background). After it finished downloading, &lt;strong&gt;only then it paused the HTML parsing and start executing the script file&lt;/strong&gt; but before the window’s &lt;code&gt;load&lt;/code&gt; event.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;async&lt;/code&gt; attribute &lt;strong&gt;does not guarantee the order of execution of script files&lt;/strong&gt;. The script files will be executed asynchronously or in random order.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt; defer attributes
&lt;/h3&gt;

&lt;p&gt;The &lt;code&gt;defer&lt;/code&gt; attribute also downloads the script file during HTML parsing( in the background) but &lt;strong&gt;will only execute it after the HTML parsing is completed&lt;/strong&gt; but before DOMContentLoaded event.&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;defer&lt;/code&gt; in the script tag also &lt;strong&gt;assured that the scripts will be executed in the same order as they appear in the file&lt;/strong&gt;. This is very much useful in the scenario when one script depends on another script.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;This is how it all works:&lt;/strong&gt; 👇&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--9VX-T7qn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9e2wb430ezkn7jcfzbra.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--9VX-T7qn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9e2wb430ezkn7jcfzbra.jpeg" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Similarity
&lt;/h3&gt;

&lt;p&gt;Both defer and async attribute downloads the script in &lt;strong&gt;background&lt;/strong&gt; while the HTML parsing is going on. &lt;strong&gt;Downloading of scripts this way does not block the rendering of DOM and as a result&lt;/strong&gt;, user can see the webpage instead of a white screen.&lt;/p&gt;

&lt;h3&gt;
  
  
  Difference
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;defer&lt;/code&gt; :- Script execution starts after HTML parsing is finished but before the &lt;code&gt;DOMContentLoaded&lt;/code&gt; event.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;async&lt;/code&gt; :- Scripts execute after they finish downloading but before the window’s &lt;code&gt;load&lt;/code&gt; event.&lt;/p&gt;

&lt;h3&gt;
  
  
  Where async and defer use?
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;If the script files are dependent on each other, then use &lt;code&gt;defer&lt;/code&gt; attribute.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;async&lt;/code&gt; attribute is useful when we don’t care when the script loads and scripts do not rely on each other.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;defer&lt;/code&gt; maintains the order of execution of script but &lt;code&gt;async&lt;/code&gt; doesn't.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Congratulations for reading until the end! In this article you’ve learned:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Javascript async and defer&lt;/li&gt;
&lt;li&gt;Similarity and difference and where to use of async and defer attributes in script tag.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Suggestions are highly appreciated ❤️
&lt;/h4&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Understanding of DOM(Document Object Model), Shadow DOM, Virtual DOM.</title>
      <dc:creator>K.Saravanakumar</dc:creator>
      <pubDate>Sat, 14 Aug 2021 02:16:44 +0000</pubDate>
      <link>https://dev.to/saravanakumarke/understanding-of-dom-document-object-model-shadow-dom-virtual-dom-3n19</link>
      <guid>https://dev.to/saravanakumarke/understanding-of-dom-document-object-model-shadow-dom-virtual-dom-3n19</guid>
      <description>&lt;p&gt;DOM (Document Object Model) is a fundamental concept in front-end, and for sure, everyone who tried to learn programming has heard about it more than once. For beginners, it’s not so easy to understand what it exactly is and how to manipulate it. DOM manipulation is most important and that brings lot of issue with performance.&lt;br&gt;
Nowadays, there are two essential concepts of DOM came with web frameworks like React.js or Vue.js or Angular, such as &lt;strong&gt;Shadow DOM and Virtual DOM&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Here’s what we’ll cover in this article:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;what is the Document Object Model?&lt;/li&gt;
&lt;li&gt;what is Shadow DOM?&lt;/li&gt;
&lt;li&gt;what is Virtual DOM?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So let’s dive in!&lt;/p&gt;

&lt;h3&gt;
  
  
  what is the Document Object Model?
&lt;/h3&gt;

&lt;p&gt;Beginners often mislead DOM with what they create as an HTML structure of their website or application. But HTML becomes the DOM after the browser parses it, then it becomes the DOM.&lt;/p&gt;

&lt;p&gt;The definition says that DOM is an API for HTML or XML documents and it creates a logical structure which can be accessed and manipulated. In other words, Javascript can access and do changes in the Document Object Model.&lt;/p&gt;

&lt;p&gt;The reason to implement the Document Object Model was to provide a standard programming interface, which could be used with any programming language in different environments. By DOM modification, we can understand adding, delete, or modify the elements of the website, assigning them different behavior, etc.&lt;/p&gt;

&lt;p&gt;Every browser has its global object, called window. Inside the window, there are different properties and methods. One of the properties in the window object is a document where we may find lots of properties and methods which can be used to access the DOM elements to interact with them.&lt;/p&gt;

&lt;p&gt;The graphic representation of the DOM looks like below:👇&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--awY3s03l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lbtb4s9pe3clok2vcp8u.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--awY3s03l--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lbtb4s9pe3clok2vcp8u.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, it’s created as a tree. It starts from the main document object, then the html object is created, and html element leads to head and body, and so on. Each of the objects represents an HTML element from the website as an object, with properties, attributes, and methods which allows to modify it.&lt;/p&gt;

&lt;h3&gt;
  
  
  what is Shadow DOM?
&lt;/h3&gt;

&lt;p&gt;Shadow DOM is a tool used to build component-based apps and websites. Shadow DOM comes in small pieces, and it doesn’t represent the whole Document Object Model. We can see it as a subtree or as a separate DOM for an element. Shadow DOM can be imaged like bricks from which the DOM is created.&lt;/p&gt;

&lt;p&gt;The main difference between DOM and Shadow DOM is how it’s created and how it behaves. Normally DOM nodes which we create are placed inside other elements, like in the tree we saw before. In the case of Shadow DOM, we create a scoped tree, which is connected to the element but separated from the children elements. It’s called shadow tree and the element it’s attached to is called shadow host.&lt;/p&gt;

&lt;p&gt;Let look at example : 👇&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--itMW7uEE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hdj3a87ykryc4m3p6e1n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--itMW7uEE--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hdj3a87ykryc4m3p6e1n.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here we come to a great advantage of the Shadow DOM, everything which we will add to Shadow DOM is local, even styles. Let’s explain why Shadow DOM is so useful and what issues it solves. First of all, it isolates the DOM, so the DOM of the component is a separate element which won’t appear in a global DOM.&lt;/p&gt;

&lt;p&gt;☞ It helps with is scoping of the CSS, which means styles created inside the single Shadow DOM element are isolated and stays in the scope of that Shadow DOM. It simplifies styling a lot as we don’t have to worry a lot about naming space and we can use simple selectors and class names.&lt;/p&gt;

&lt;p&gt;☞ Shadow DOM can affect the performance of the application. As said at the beginning of the article, there are a lot of performance issues while we want to manipulate the DOM, because every change will make a re-rendering of the whole object. In the case of Shadow DOM browser knows which part should be updated.&lt;/p&gt;

&lt;h3&gt;
  
  
  Take a break
&lt;/h3&gt;

&lt;p&gt;So far so good — take a break!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sFjyaYRS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jxg24xm76gk7w85do7ye.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sFjyaYRS--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jxg24xm76gk7w85do7ye.png" alt="alt text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  what is Virtual DOM?
&lt;/h3&gt;

&lt;p&gt;Virtual DOM is a concept of DOM used by React.js and Vue.js. In Virtual DOM concept copy of DOM is saved in the memory and while any change is done in the DOM, it’s compared to find differences. Then browser knows which elements were changed and can update only those part of the application to avoid re-rendering all the DOM. It’s done to improve the performance of the UI libraries.&lt;/p&gt;

&lt;p&gt;Let look at example : 👇&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mXp1Pndp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/en422ihmmlwvtnqqnhnw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mXp1Pndp--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/en422ihmmlwvtnqqnhnw.png" alt="alt text"&gt;&lt;/a&gt;&lt;br&gt;
Let look at how it work : 👇&lt;/p&gt;

&lt;p&gt;As we know, form the above in DOM, every element is re-rendered, no matter if it was changed or not. Let’s check in depth how Virtual DOM works step by step. So first, the change is done, and it’s done to the Virtual DOM, not to the original DOM, then the Virtual DOM is compared with the Document Object Model, and this process is called “diffing”. While &lt;strong&gt;the differences are found then browser know which elements in the original DOM should be updated and the update is done&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In the Virtual DOM concept, it’s possible to apply more than one change at once, to avoid re-rendering for every single element change. The biggest issue that Virtual DOM solves is the performance improvement on DOM manipulation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Differences between Shadow DOM and Virtual DOM
&lt;/h3&gt;

&lt;p&gt;The only thing which is &lt;strong&gt;common&lt;/strong&gt; for both is that they help with &lt;strong&gt;performance issues&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Both create a separate instance of the Document Object Model; besides this, both concepts are different. Virtual DOM is creating a &lt;strong&gt;copy of the whole DOM object&lt;/strong&gt;, and Shadow DOM creates &lt;strong&gt;small pieces of the DOM object which has their own&lt;/strong&gt;, isolated scope for the element they represent.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;Congratulations for reading until the end! In this article you’ve learned:&lt;/p&gt;

&lt;p&gt;➣ What is DOM&lt;br&gt;
➣ What is shadow DOM and virtual DOM&lt;br&gt;
➣ Difference between Shadow DOM and virtual DOM&lt;/p&gt;

&lt;p&gt;I hope you find this article helpful in understanding what the DOM is and how Virtual and Shadow DOM concepts work.&lt;/p&gt;

&lt;h3&gt;
  
  
  Suggestions and criticisms are highly appreciated ❤️
&lt;/h3&gt;

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