<?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>Forem: Majedur Rahman</title>
    <description>The latest articles on Forem by Majedur Rahman (@rahmanmajeed).</description>
    <link>https://forem.com/rahmanmajeed</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%2F320114%2Fe6440398-bc59-4e61-b2e7-ebade5d0df0d.jpg</url>
      <title>Forem: Majedur Rahman</title>
      <link>https://forem.com/rahmanmajeed</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/rahmanmajeed"/>
    <language>en</language>
    <item>
      <title>Concurrent Rendering in React ⛽🛣️</title>
      <dc:creator>Majedur Rahman</dc:creator>
      <pubDate>Sun, 28 Apr 2024 03:37:47 +0000</pubDate>
      <link>https://forem.com/rahmanmajeed/concurrent-rendering-in-react-j1d</link>
      <guid>https://forem.com/rahmanmajeed/concurrent-rendering-in-react-j1d</guid>
      <description>&lt;p&gt;In this post, we will discuss the concept of &lt;em&gt;concurrent&lt;/em&gt; rendering in &lt;strong&gt;React 18&lt;/strong&gt;, which allows us to &lt;strong&gt;improve&lt;/strong&gt; user experience in new and exciting ways.&lt;/p&gt;

&lt;p&gt;So, buckle up, and let's dive into the world of &lt;em&gt;concurrency&lt;/em&gt; in React!&lt;/p&gt;

&lt;h4&gt;
  
  
  What is Concurrency ?
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Concurrency&lt;/strong&gt; means &lt;em&gt;executing multiple tasks at the same time but not necessarily &lt;strong&gt;simultaneously&lt;/strong&gt;&lt;/em&gt;. In a concurrent application, two tasks can start, run and complete in overlapping time periods, without waiting for one to finish before starting another. i.e Task-2 can start even before Task-1 gets completed.&lt;br&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%2Fuploads%2Farticles%2Fmmldrkws0b3q2lf1g1x0.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%2Fuploads%2Farticles%2Fmmldrkws0b3q2lf1g1x0.png" alt="Concurrency" width="800" height="215"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Concurrency in React, Explained Simply
&lt;/h3&gt;

&lt;p&gt;In the context of React.js, concurrency refers to having more than one task in progress at once without blocking the main thread, and concurrent tasks can overlap depending on which is more urgent. This is a stark contrast to the traditional, synchronous rendering model where React would block the main thread until it finished rendering the component tree.&lt;/p&gt;

&lt;p&gt;For example, while you're writing, you're also preparing vegetables to eat. When the time comes to add ingredients to the vegetables, that will be more urgent, so you'll pause writing and attend to that and come back to continue writing when you're done. &lt;br&gt;
At different points throughout the writing and cooking processes, your focus will be on what is more urgent.&lt;/p&gt;

&lt;p&gt;React could only handle one task at a time in the past, and a task could not be interrupted once it had started. This approach is referred to as &lt;strong&gt;Blocking Rendering&lt;/strong&gt;. To fix this problem, &lt;strong&gt;Concurrent Mode&lt;/strong&gt; was introduced—which makes rendering interruptible.&lt;/p&gt;
&lt;h4&gt;
  
  
  What is Concurrent Rendering in React ?
&lt;/h4&gt;

&lt;p&gt;In the realm of React, concurrent rendering is a game-changer. It's a new way of rendering that helps your app stay responsive under heavy load and gives you more control over how updates get scheduled.&lt;br&gt;
Concurrent rendering is all about creating a fluid user experience. It allows React to interrupt an ongoing rendering process to handle more urgent tasks. This way, even if your app is in middle of a large rendering task, it can still respond immediately to user interactions.&lt;/p&gt;
&lt;h4&gt;
  
  
  Say Bye to Concurrent Mode
&lt;/h4&gt;

&lt;p&gt;Concurrent Mode was introduced as an experimental feature. In favor of a more logical adoption that allows you to opt in to concurrent rendering at your conveniences. &lt;em&gt;&lt;strong&gt;Concurrent Mode&lt;/strong&gt;&lt;/em&gt; is now being replaced in React 18 with &lt;em&gt;&lt;strong&gt;concurrent features&lt;/strong&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Here's an example to illustrate the 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;// React 16&lt;/span&gt;
&lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="c1"&gt;// React 18&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ReactDOM&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createRoot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;root&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;App&lt;/span&gt; &lt;span class="o"&gt;/&amp;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 React 16, we use &lt;strong&gt;ReactDOM.render&lt;/strong&gt; to render our app. &lt;br&gt;
In React 18, we use &lt;strong&gt;ReactDOM.createRoot&lt;/strong&gt; to create a root node and then call render on that root node. This enables concurrent rendering for the entire app.&lt;/p&gt;
&lt;h2&gt;
  
  
  Concurrent Features in React 18
&lt;/h2&gt;

&lt;p&gt;Let’s look at a few more concurrency features that React 18 introduces.&lt;/p&gt;
&lt;h3&gt;
  
  
  Automatic batching
&lt;/h3&gt;

&lt;p&gt;React 18 introduces great key feature is automatic batching. which enhances rendering effectiveness, performance and responsiveness. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Batching&lt;/strong&gt; is when React groups multiple state updates into a single re-render for better performance.&lt;/p&gt;

&lt;p&gt;In React 17 and prior, were &lt;strong&gt;&lt;em&gt;batched&lt;/em&gt;&lt;/strong&gt; update occur only the state updates defined inside React event handlers. But updates inside of promises, setTimeout, native events handlers, or any other event were not &lt;em&gt;&lt;strong&gt;batched&lt;/strong&gt;&lt;/em&gt; in React by default.&lt;/p&gt;

&lt;p&gt;Here's the example, React 17 and prior versions:&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;increase&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setIncrease&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;decrease&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setDecrease&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleOnClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="nf"&gt;setIncrease&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;increase&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
     &lt;span class="nf"&gt;setDecrease&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;decrease&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Rendering&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;wrapper&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Counter&lt;/span&gt; &lt;span class="nx"&gt;details&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nx"&gt;Increase&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;strong&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;increase&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/strong&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nx"&gt;Decrease&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;strong&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;decrease&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/strong&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleOnClick&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Click&lt;/span&gt; &lt;span class="nx"&gt;Me&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;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 the above example, the &lt;code&gt;handleOnClick()&lt;/code&gt; function will be called when a user clicks on the button. It will execute two-state updates on each click.&lt;/p&gt;

&lt;p&gt;If you observe the browser console, you will see that the &lt;strong&gt;“Rendering”&lt;/strong&gt; message is logged only &lt;strong&gt;once&lt;/strong&gt; for &lt;em&gt;&lt;strong&gt;both state updates&lt;/strong&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Now you have seen that React &lt;strong&gt;batched&lt;/strong&gt; updates both states and re-rendered the component only &lt;strong&gt;once&lt;/strong&gt; inside &lt;strong&gt;event handler&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;But, what if we execute state updates in a context that is not associated with the browser?&lt;/p&gt;

&lt;p&gt;For example, consider a &lt;code&gt;setTimeout()&lt;/code&gt; call that asynchronously loads data:&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;increase&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setIncrease&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;decrease&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setDecrease&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleOnClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// updates state inside of setTimeout&lt;/span&gt;
    &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;setIncrease&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;increase&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="nf"&gt;setDecrease&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;decrease&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;0&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Rendering&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;wrapper&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Counter&lt;/span&gt; &lt;span class="nx"&gt;details&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nx"&gt;Increase&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;strong&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;increase&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/strong&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nx"&gt;Decrease&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;strong&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;decrease&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/strong&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleOnClick&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Click&lt;/span&gt; &lt;span class="nx"&gt;Me&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;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 the above example, the state update occurs in the callback of &lt;code&gt;setTimeout()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;If you observe the browser console after executing this example, you will see 2 messages. This indicates that &lt;strong&gt;two separate re-renders&lt;/strong&gt; occur for &lt;strong&gt;each state update&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This behaviour can cause severe performance issues related to application speed. That’s why React introduced &lt;strong&gt;Automatic Batching&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;React 18&lt;/strong&gt; we are introduced to improved version of batching called &lt;strong&gt;Automatic Batching&lt;/strong&gt;. It will enabled batching of all the state updates regardless of where they are called. This includes updates inside promises, timeouts, and native event handlers.&lt;/p&gt;

&lt;p&gt;Let’s consider the previous example to understand how &lt;strong&gt;automatic batching&lt;/strong&gt; works:&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;increase&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setIncrease&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;decrease&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setDecrease&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleOnClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// updates state inside of setTimeout&lt;/span&gt;
    &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nf"&gt;setIncrease&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;increase&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="nf"&gt;setDecrease&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;decrease&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;0&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Rendering&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt; &lt;span class="nx"&gt;className&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;wrapper&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Counter&lt;/span&gt; &lt;span class="nx"&gt;details&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nx"&gt;Increase&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;strong&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;increase&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/strong&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nx"&gt;Decrease&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;strong&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;decrease&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/strong&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleOnClick&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Click&lt;/span&gt; &lt;span class="nx"&gt;Me&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above example shows an optimal re-rendering process where &lt;code&gt;handleOnClick()&lt;/code&gt; function will be called when a user clicks on the button.It will execute two-state updates on each click and give only &lt;em&gt;&lt;strong&gt;one re-render&lt;/strong&gt;&lt;/em&gt; as React batches all state updates regardless of the invocation location.&lt;/p&gt;

&lt;p&gt;However, Automatic batching significantly reduces rendering iterations, which enhances performance and responsiveness.&lt;/p&gt;

&lt;h3&gt;
  
  
  SSR with Suspense
&lt;/h3&gt;

&lt;p&gt;Server-side rendering is a technique that allows us to generate HTML from React components on the server and then send a fully rendered HTML page to client. After the HTML is rendered in the browser, the React and JavaScript code for the entire app starts loading. When it is done, It adds interactivity to static HTML generated on the server—a process known as &lt;strong&gt;hydration&lt;/strong&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Why Suspense ?
&lt;/h4&gt;

&lt;p&gt;In the previous versions of React, hydration could only begin after the entire data had been fetched from the server and rendered to HTML. Additionally, your users couldn’t interact with the page until hydration was complete for the whole page.&lt;/p&gt;

&lt;p&gt;To solve this problem, the &lt;strong&gt;&lt;/strong&gt; component was released in 2018. The only supported use case was &lt;strong&gt;lazy-loading&lt;/strong&gt; code on the client, not during server rendering. &lt;/p&gt;

&lt;p&gt;Let’s start with how the &lt;code&gt;&amp;lt;Suspense&amp;gt;&lt;/code&gt; component works with the lazy loading components.&lt;/p&gt;

&lt;p&gt;When using the Suspense component, you must provide a fallback option. It accepts any React components or even a string to render while the lazy component is loading.&lt;/p&gt;

&lt;p&gt;Take a look at the example below, which uses the Suspense wrapper to display a typical loader until the &lt;code&gt;&amp;lt;LazyLoadComponent&amp;gt;&lt;/code&gt; is ready.&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;lazy&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Suspense&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;LazyLoadComponent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;lazy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./LazyLoadComponent&lt;/span&gt;&lt;span class="dl"&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;function&lt;/span&gt; &lt;span class="nf"&gt;MyComponent&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="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Suspense&lt;/span&gt; &lt;span class="nx"&gt;fallback&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Loader&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;LazyLoadComponent&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Suspense&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&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;Now, let’s take a look at the enhancements Suspense received in React 18.&lt;/p&gt;

&lt;p&gt;Suspense is no longer limited to lazy components, React 18 added support for Suspense on the server. React 18 offers two major features for SSR unlocked by Suspense:&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Streaming Server Rendering
&lt;/h4&gt;

&lt;p&gt;React 18 introduces new server rendering APIs that allow you to stream HTML from the server to the client. This means &lt;strong&gt;Streaming HTML&lt;/strong&gt; is just sending lightweight static HTML. It’s replaced by heavier components that depend, without waiting for the entire response.&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="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Layout&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Article&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Suspense&lt;/span&gt; &lt;span class="nx"&gt;fallback&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Spinner&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Comments&lt;/span&gt; &lt;span class="o"&gt;/&amp;gt;&lt;/span&gt;
  &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Suspense&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/Layout&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  2. Selective hydration
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Selective hydration&lt;/strong&gt; mechanisms decide which components need to be loaded first. React detects events started by the user’s interaction with the component and hydrate certain components first.&lt;/p&gt;

&lt;p&gt;If you have multiple Suspense, React will attempt to hydrate all of them but it will start with Suspense found earlier in the tree first. &lt;/p&gt;

&lt;h3&gt;
  
  
  Transitions
&lt;/h3&gt;

&lt;p&gt;A &lt;em&gt;transition&lt;/em&gt; is a new concept in React to distinguish between &lt;em&gt;urgent&lt;/em&gt; and &lt;em&gt;non-urgent&lt;/em&gt; updates.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Urgent updates&lt;/strong&gt; reflect direct interaction, like typing, clicking, pressing, and so on.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Transition updates&lt;/strong&gt; transition the UI from one view to another.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Transitions API gives us an &lt;strong&gt;imperative&lt;/strong&gt; way of marking some update as low priority.&lt;/p&gt;

&lt;h4&gt;
  
  
  StartTransition API
&lt;/h4&gt;

&lt;p&gt;The &lt;strong&gt;startTransition&lt;/strong&gt; API can be used to mark any state updates as non-urgent. The &lt;code&gt;startTransition&lt;/code&gt; API receives a function as argument such that any state updates that occur within this function are marked as a low priority update.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;startTransition: a method to start transitions when the hook cannot be used.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the code block below, there are two state updates- one that is urgent and other is marked as non-urgent as it's wrapped inside the &lt;code&gt;startTransition&lt;/code&gt; callback:&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;startTransition&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Urgent: Show what was typed&lt;/span&gt;
&lt;span class="nf"&gt;setInputValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Mark any state updates inside as transitions (non-urgent)&lt;/span&gt;
&lt;span class="nf"&gt;startTransition&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Transition: Show the results&lt;/span&gt;
  &lt;span class="nf"&gt;setResults&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;input&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;Non-urgent updates will be &lt;strong&gt;interrupted&lt;/strong&gt; if more urgent updates like &lt;code&gt;clicks&lt;/code&gt; or &lt;code&gt;key presses&lt;/code&gt; come in. If a transition gets interrupted by the user (for example, by typing multiple characters in a row) or start transition is called again before the previous call is completed, React by default cancels older transitions and render only the latest update.&lt;/p&gt;

&lt;h4&gt;
  
  
  useTransition Hook
&lt;/h4&gt;

&lt;p&gt;React can also track and update pending state transitions using the &lt;code&gt;useTransition&lt;/code&gt; hook.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;useTransition: a hook to start transitions, including a value to track the pending state.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Invoking &lt;code&gt;const[isPending, startTransition] = useTransition()&lt;/code&gt; returns an array of &lt;strong&gt;two&lt;/strong&gt; items:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;isPending&lt;/code&gt;: indicates that the transition is pending.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;startTransition(callback)&lt;/code&gt;: allows you to mark any UI updates inside &lt;code&gt;callback&lt;/code&gt; as transitions or which dispatches a new concurrent render.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As already mentioned, you can use &lt;code&gt;useTransition()&lt;/code&gt; hook to let know React which UI updates are urgent (like updating the input field value), and which are non-urgent transitions (like updating the names list to highlight the query).&lt;/p&gt;

&lt;p&gt;First, let's invoke the &lt;code&gt;[isPending, startTransition] = useTransition()&lt;/code&gt; hook to get access to &lt;code&gt;startTransition()&lt;/code&gt; function. Secondly, let's create a state variable to hold the query state value specifically for the transition.&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="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useTransition&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&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;MyCounter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;isPending&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;startTransition&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useTransition&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;increment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useCallback&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;startTransition&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// Run this update concurrently&lt;/span&gt;
            &lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="o"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Count&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;span&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;isPending&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Pending&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Not Pending&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/span&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;            &lt;span class="c1"&gt;// Component which benefits from concurrency&lt;/span&gt;
            &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ManySlowComponents&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;        &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&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;An important caveat of &lt;code&gt;useTransition&lt;/code&gt; is that it cannot be used for controlled inputs.&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="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setText&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;''&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;handleChange&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ❌ Can't use Transitions for controlled input state&lt;/span&gt;
  &lt;span class="nf"&gt;startTransition&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&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;// ...&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;input&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="nx"&gt;onChange&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleChange&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="sr"&gt;/&amp;gt;&lt;/span&gt;&lt;span class="err"&gt;;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is because Transitions are non-blocking, but updating an input in response to the change event should happen synchronously. If you want to run a Transition in response to typing, you have two options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You can declare two separate state variables: one for the input state (which always updates synchronously), and one that you will update in a Transition. This lets you control the input using the synchronous state, and pass the Transition state variable (which will “lag behind” the input) to the rest of your rendering logic.&lt;/li&gt;
&lt;li&gt;Alternatively, you can have one state variable, and add useDeferredValue which will “lag behind” the real value. It will trigger non-blocking re-renders to “catch up” with the new value automatically.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For those cases, it is best to use &lt;code&gt;useDeferredValue.&lt;/code&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  useDeferredValue Hook
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;useDeferredValue&lt;/code&gt; hook is a React hook that allows you to defer updating a part of the UI. It is a convenient hook for cases where you do not have the opportunity to wrap the state update in &lt;code&gt;startTransition&lt;/code&gt; but still wish to run the update concurrently.&lt;/p&gt;

&lt;p&gt;An example of where this occurs is child components receiving a new value from the parent.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;useDeferredValue&lt;/code&gt; hook is a useful addition to React 18, aimed to improve component rendering performance by preventing unnecessary re-renders.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;useDeferredValue&lt;/code&gt; hook takes two arguments:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;value&lt;/strong&gt;: The value you want to defer. It can have any type.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;options&lt;/strong&gt;: An object that can have the 2 properties &lt;strong&gt;timeoutMs&lt;/strong&gt; and &lt;strong&gt;scheduler&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;timeoutMs&lt;/strong&gt;: The time in milliseconds to wait before updating the value.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;scheduler&lt;/strong&gt;: A function that will be used to schedule the update.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;code&gt;useDeferredValue&lt;/code&gt; hook returns a value that is the deferred version of the value you provided. The deferred value will be updated after the specified amount of time, or immediately if the value changes.&lt;/p&gt;

&lt;p&gt;Conceptually, &lt;code&gt;useDeferredValue&lt;/code&gt; similar to debouncing, but has a few advantages compared to it. There is no fixed time delay, so React will attempt the deferred render right after the first render is reflected on the screen. The deferred render is interruptible and doesn’t block user input. &lt;/p&gt;

&lt;p&gt;Here is an example of how to use the &lt;code&gt;useDeferredValue&lt;/code&gt; hook:&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useDeferredValue&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;deferredCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useDeferredValue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;timeoutMs&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;handleClick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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="p"&gt;(&lt;/span&gt;
    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;div&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;h1&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Counter&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/h1&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Current&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;button&lt;/span&gt; &lt;span class="nx"&gt;onClick&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;handleClick&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Click&lt;/span&gt; &lt;span class="nx"&gt;me&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/button&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="nx"&gt;Deferred&lt;/span&gt; &lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;deferredCount&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/p&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/div&lt;/span&gt;&lt;span class="err"&gt;&amp;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 example, the &lt;code&gt;count&lt;/code&gt; state variable is updated immediately when the user clicks the button. However, the &lt;code&gt;deferredCount&lt;/code&gt; value is not updated immediately. Instead, it is updated after 1000 milliseconds. This means that the &lt;code&gt;deferredCount&lt;/code&gt; value will not reflect the latest value of the &lt;code&gt;count&lt;/code&gt; state variable until after 1000 milliseconds have passed.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Future of React with Concurrent Mode
&lt;/h2&gt;

&lt;p&gt;Concurrent mode is not just a new feature, it's a whole new way of thinking about React. It allows React to work on multiple tasks at once, without blocking the main thread. It introduces powerful new features like automatic batching, streaming server rendering, and React Suspense. And it changes how we think about rendering and state updates in React.&lt;/p&gt;

&lt;p&gt;So, as we look to the future of React with concurrent mode. Use these new powers to create more responsive, user-friendly apps.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>frontend</category>
    </item>
    <item>
      <title>JavaScript: Iterators and Generators (Part-1)💡📌</title>
      <dc:creator>Majedur Rahman</dc:creator>
      <pubDate>Wed, 17 Jan 2024 02:16:02 +0000</pubDate>
      <link>https://forem.com/rahmanmajeed/javascript-iterators-and-generators-part-1-4gj9</link>
      <guid>https://forem.com/rahmanmajeed/javascript-iterators-and-generators-part-1-4gj9</guid>
      <description>&lt;p&gt;In JavaScript, iterating through data set or collections objects such as &lt;code&gt;arrays&lt;/code&gt; is a very common operation. JavaScript provides a number of ways of iterating over a data-set or collection, from simple &lt;code&gt;for&lt;/code&gt;loops to &lt;code&gt;Array.prototype.map()&lt;/code&gt; etc.&lt;br&gt;
ES6 introduces a new mechanism for traversing data: &lt;em&gt;iteration&lt;/em&gt;. These concepts are central to iteration:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Iterable&lt;/strong&gt; is a data structure that can be iterated over through iteration.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Iterator&lt;/strong&gt; is a pointer to the next element in the iteration.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Iterability&lt;/strong&gt; is an &lt;strong&gt;event&lt;/strong&gt; where &lt;strong&gt;&lt;em&gt;data consumers&lt;/em&gt;&lt;/strong&gt; get their data from &lt;strong&gt;Iterable&lt;/strong&gt; &lt;em&gt;&lt;strong&gt;data sources&lt;/strong&gt;&lt;/em&gt; with the help of &lt;strong&gt;Iterator&lt;/strong&gt;.
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foj3nt57yqk86gvo8also.png" alt="Interface Iterable"&gt;
Practically not possible to create consumers for all data sources, that’s the reason ES6 introduces &lt;em&gt;Iterable&lt;/em&gt; interface/protocol.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So Iterators and Generators provide a mechanism for customizing the behavior of control structures or data-consumers such as &lt;u&gt;&lt;code&gt;for...of&lt;/code&gt;&lt;/u&gt; loop in JavaScript to iterate over collections in a simple and readable manner. Built-in JavaScript functions and methods, such as &lt;code&gt;Array.prototype.map()&lt;/code&gt; or &lt;code&gt;Array.prototype.forEach()&lt;/code&gt; to process collections internally use Iterators.&lt;/p&gt;

&lt;p&gt;In this article we will cover the below concepts :&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Iterators.&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;2. Iterators Interface/Protocol.&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;3. Iterables.&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;4. Iterables Interface/Protocol.&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;5. Custom Iterators.&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;6. Custom Iterables.&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;7. Iterators vs Iterables.&lt;/strong&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  Iterators.
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What is Iterators?&lt;/strong&gt;&lt;br&gt;
An Iterator is a structured pattern for pulling information from a source in one-at-a-time fashion. Conceptually, an "iterator" is an object that provides a sequence of values one-at-a-time when requested.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Long story short, iterator is a closure which returns an object having function next(), on each subsequent calling of the next function we get the next iteration value&lt;/em&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuue06q117ybfjwmcuz6h.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuue06q117ybfjwmcuz6h.png" alt="Iterator"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Principle of an Iterator&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;next() : &lt;code&gt;next&lt;/code&gt; function returns an object consisting of two keys &lt;code&gt;{value , done}&lt;/code&gt;. &lt;/li&gt;
&lt;li&gt;{value , done} : Each &lt;code&gt;next&lt;/code&gt; execution returns a &lt;code&gt;value&lt;/code&gt; but &lt;code&gt;done&lt;/code&gt; is returned as &lt;code&gt;false&lt;/code&gt; until the &lt;em&gt;iteration&lt;/em&gt; is complete(reaches length of the array/ object). Once all the &lt;code&gt;values&lt;/code&gt; are &lt;em&gt;iterated&lt;/em&gt; the &lt;code&gt;done&lt;/code&gt; is returned as &lt;code&gt;true&lt;/code&gt; and &lt;code&gt;value&lt;/code&gt; turns out to be &lt;code&gt;undefined&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Symbol.iterator: This &lt;code&gt;[Symbol.iterator]&lt;/code&gt; is a well-known symbol in JavaScript that defines the default &lt;em&gt;iterator&lt;/em&gt; for an object. It is typically implemented as a function that returns the &lt;em&gt;iterator&lt;/em&gt; object itself.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7xnxm5jipabyzjvbkbgz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7xnxm5jipabyzjvbkbgz.png" alt="Iterator Object"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To use an iterator you typically call the &lt;code&gt;Symbol.iterator&lt;/code&gt; method on a collection or data structure to obtain an iterator object.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjoaox4tvc573f8p1ogqm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjoaox4tvc573f8p1ogqm.png" alt="Iterator example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Iterators Interface/Protocol
&lt;/h2&gt;

&lt;p&gt;The concept of the protocol can be split into two. The &lt;em&gt;iterable&lt;/em&gt;, the data structure itself, and the &lt;em&gt;iterator&lt;/em&gt;, a &lt;strong&gt;pointer&lt;/strong&gt; that moves over the &lt;strong&gt;iterable&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;iterator&lt;/em&gt; interface or protocol defines an standard to produce a sequence of values. This &lt;code&gt;protocol&lt;/code&gt; is really an object that contains a &lt;strong&gt;next()&lt;/strong&gt; method and this &lt;strong&gt;next()&lt;/strong&gt; method returns another object with two properties, &lt;strong&gt;value&lt;/strong&gt; and &lt;strong&gt;done&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffkj1jd0h62mg83wycpus.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffkj1jd0h62mg83wycpus.png" alt="Iterator Protocol"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;next()&lt;/strong&gt; method always returns an object with &lt;strong&gt;value&lt;/strong&gt; and &lt;strong&gt;done&lt;/strong&gt; properties. The &lt;strong&gt;value&lt;/strong&gt; property contains the current value of the iteration and the &lt;strong&gt;done&lt;/strong&gt; property indicates the termination of the &lt;em&gt;iterator&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;whenever, any &lt;code&gt;data-source&lt;/code&gt; implement the &lt;code&gt;[Symbol.iterator]&lt;/code&gt; protocol that produces an &lt;em&gt;iterator&lt;/em&gt; and makes the &lt;code&gt;data-structure&lt;/code&gt; &lt;em&gt;iterable&lt;/em&gt; whenever the data structure in placed in a loop. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvt5dt2kc1wwmkhp7gav5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvt5dt2kc1wwmkhp7gav5.png" alt="Iterator Protocol1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Key points about &lt;code&gt;Symbol.iterator&lt;/code&gt; :&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;value of this is a function.&lt;/li&gt;
&lt;li&gt;when we call the function associated with &lt;code&gt;Symbol.iterator&lt;/code&gt; , it gives us a &lt;code&gt;iterator object&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;iterator object&lt;/strong&gt; returned from &lt;code&gt;Symbol.iterator&lt;/code&gt; method has &lt;code&gt;.next&lt;/code&gt; method associated it it.&lt;/li&gt;
&lt;li&gt;And one each &lt;code&gt;next()&lt;/code&gt; call we get the next value along the &lt;code&gt;done&lt;/code&gt; status.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk5lmak69wnohn48kddfr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk5lmak69wnohn48kddfr.png" alt="Itr example"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We have to iterate one extra time, then only we get &lt;code&gt;done:true&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Custom Iterators&lt;/strong&gt;&lt;br&gt;
Now, let’s create an custom iterator that returns only the  administrators' names. consider &lt;code&gt;true&lt;/code&gt; values are admin.&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&lt;p&gt;const users = {&lt;br&gt;
    james: false,&lt;br&gt;
    andrew: true,&lt;br&gt;
    alexander: false,&lt;br&gt;
    daisy: false,&lt;br&gt;
    luke: false,&lt;br&gt;
    clare: true,&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Symbol.iterator]() {
    const keys = Object.keys(this);
    let index = 0;

    const iterator = {
         // each call will return a new iterator
        next: () =&amp;amp;gt; {
            while (!this[keys[index]] &amp;amp;amp;&amp;amp;amp; index &amp;amp;lt; keys.length) index++;


            // here the relevant part
            return { 
                done: index &amp;amp;gt;= keys.length,
                // after reading the name corresponding to the current index,
                // do not forget to move forward the 'index'
                // for the next iteration
                value: keys[index++],
            };
        }
    }

    return iterator;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;}&lt;/p&gt;

&lt;p&gt;for (let name of users) {&lt;br&gt;
  console.log(name); //"andrew","clare"&lt;br&gt;
}&lt;/p&gt;

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

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Iterables.&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What is Iterable?&lt;/strong&gt;&lt;br&gt;
In general, it is a &lt;code&gt;data structure&lt;/code&gt; that allows its data to be consumed. It does so by implementing a method whose key is &lt;code&gt;Symbol.iterator&lt;/code&gt; which returns an iterator.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;From a technical perspective, an “iterable” isn’t a specific data type in JS. Rather, it’s a protocol that various data types and objects can implement and the JS engine will treat such values as iterables.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Principle of an Iterable&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;To call any object iterable, it needs to implement the iterable interface.&lt;/li&gt;
&lt;li&gt;The &lt;em&gt;iterable&lt;/em&gt; object must have a &lt;code&gt;@@iterator&lt;/code&gt; method that returns an &lt;em&gt;iterator&lt;/em&gt; object. The &lt;code&gt;@@iterator&lt;/code&gt; key is a symbol that can be accessed via &lt;code&gt;Symbol.iterator&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;The property &lt;code&gt;Symbol.Iterator&lt;/code&gt; on an object converts the object into Iterable.&lt;/li&gt;
&lt;li&gt;Iterables which can iterate only once (such as Generators) customarily return &lt;code&gt;this&lt;/code&gt; from their @@iterator method.&lt;/li&gt;
&lt;li&gt;Iterables which can be iterated many times must return a new iterator on each invocation of @@iterator.&lt;/li&gt;
&lt;/ul&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&lt;p&gt;let iterable ={&lt;br&gt;
    [Symbol.iterator] : () =&amp;gt; {&lt;br&gt;
        //Iterator defination&lt;br&gt;
    }&lt;br&gt;
}&lt;/p&gt;

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

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Iterable Interface/Protocol&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;The iterable protocol&lt;/strong&gt; allows JavaScript objects to define or customize their iteration behavior. By being an iterable object it needs to implement an &lt;code&gt;@@iterator&lt;/code&gt; method (an iterator key), meaning that the object (or one of the objects up its &lt;code&gt;prototype chain&lt;/code&gt;) must have a property with a &lt;code&gt;@@iterator&lt;/code&gt; key which is available via constant &lt;code&gt;Symbol.iterator&lt;/code&gt;, this method needs to returns an iterator protocol.&lt;/p&gt;

&lt;p&gt;Whenever an object needs to be iterated (such as at the beginning of a &lt;u&gt;&lt;code&gt;for...of&lt;/code&gt;&lt;/u&gt; loop), its &lt;code&gt;@@iterator&lt;/code&gt; method is called with no arguments, and the returned iterator is used to obtain the values to be iterated.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fohyzbtkaebaziml26dz9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fohyzbtkaebaziml26dz9.png" alt="Iterable protocol"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Custom Iterables.&lt;/strong&gt;&lt;br&gt;
We are writing an iterable object with will iterate over an array and print the array values.&lt;/p&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

&lt;p&gt;const iterable = {&lt;br&gt;
  numbers: [1, 2, 3, 4, 5],&lt;/p&gt;

&lt;p&gt;[Symbol.iterator]: function () {&lt;br&gt;
    let index = 0;&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return {
  next: function () {
    if (index &amp;amp;lt; this.numbers.length) {
      let temp = { value: this.numbers[index], done: false };
      index++;
      return temp;
    } else {
      return { value: undefined, done: true };
    }
  }.bind(this),
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;},&lt;br&gt;
};&lt;/p&gt;

&lt;p&gt;for (let i of iterable) {&lt;br&gt;
  console.log(i); // result is: 1,2,3,4,5&lt;br&gt;
}&lt;/p&gt;

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

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Iterators vs Iterables.&lt;br&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;As discussed above Iterable is an object which consists of an Iterator&lt;/li&gt;
&lt;li&gt;Iterator is defined under the key Symbol.Iterator.&lt;/li&gt;
&lt;li&gt;Iterator is the action definition of iteration this holds the custom logic of next() function&lt;/li&gt;
&lt;li&gt;Next function is called iteratively and as per the protocol it returns value and done keys.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;we will discuss about &lt;strong&gt;Generators&lt;/strong&gt; in upcoming blog &lt;strong&gt;JavaScript: Iterators and Generators (Part-2)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;"That's all folks!"&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  References:
&lt;/h3&gt;

&lt;p&gt;Books&lt;br&gt;
&lt;em&gt;ES6 &amp;amp; Beyond (YDKJS)-kyle simpson&lt;/em&gt;&lt;br&gt;
Images&lt;br&gt;
&lt;a href="https://www.google.com" rel="noopener noreferrer"&gt;Google&lt;/a&gt; and designed.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>webdev</category>
      <category>advancejavascript</category>
    </item>
    <item>
      <title>JavaScript: The Runtime Environment🏃🏻🏃🏻🏃🏻</title>
      <dc:creator>Majedur Rahman</dc:creator>
      <pubDate>Sat, 07 Oct 2023 17:01:34 +0000</pubDate>
      <link>https://forem.com/rahmanmajeed/javascript-the-runtime-environment-35a2</link>
      <guid>https://forem.com/rahmanmajeed/javascript-the-runtime-environment-35a2</guid>
      <description>&lt;p&gt;&lt;strong&gt;So you may know that in JavaScript, your code somehow compiles and executes in your browser or server to display the beautiful application you've built. But are you aware of all the components that come into play to produce the output?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;let's dive into deep and see what happens behind the hood in JavaScript and how it's run.&lt;/p&gt;

&lt;p&gt;first, we're just glance over the runtime definition.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What's a Runtime?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A runtime is the environment in which a programming language executes. The runtime system facilitates storing functions, variables, and managing memory by using data structures such as queues, heaps and stacks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;JavaScript Runtime Environment&lt;br&gt;
&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
JavaScript runtime environment is like a container, that has all the things to run a JavaScript Code.There are two types of JavaScript runtime environment:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The runtime environment of a &lt;strong&gt;Browser&lt;/strong&gt; (like Google Chrome).&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;Node&lt;/strong&gt; runtime environment.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Every Browser has it's own JS Runtime Environment. The heart of any JavaScript Runtime is always &lt;strong&gt;&lt;em&gt;JavaScript Engine&lt;/em&gt;&lt;/strong&gt;. Without an engine there is no runtime and no JavaScript at all.&lt;/p&gt;

&lt;p&gt;A JavaScript Runtime consists of the following components- &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;[🚀] &lt;strong&gt;&lt;em&gt;The JS Engine&lt;/em&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;[🌏] &lt;strong&gt;&lt;em&gt;Web/Global APIs&lt;/em&gt;&lt;/strong&gt;. &lt;/li&gt;
&lt;li&gt;[⌛] &lt;strong&gt;&lt;em&gt;Callback Queue&lt;/em&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;[🔬] &lt;strong&gt;&lt;em&gt;Microtask Queue&lt;/em&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;[🔄] &lt;strong&gt;&lt;em&gt;Event Loop&lt;/em&gt;&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here, we can glimpsed the JavaScript Runtime Environment as a whole in one picture.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7dlps29vwzbuskebux0b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7dlps29vwzbuskebux0b.png" alt="Image description"&gt;&lt;/a&gt;                              &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. The JS Engine 🚀&lt;/strong&gt;&lt;br&gt;
The JavaScript engine, is a vital component of the JavaScript Runtime Environment. Consider it heart of the JavaScript runtime environment. Operating in a single-threaded manner,it executes tasks one at a time, following a specific order. There are a lot of steps involved in doing that, but essentially executing JavaScript code is what an engine does.&lt;br&gt;
Every browser has its own JavaScript engine but the most well known engine is Google's V8. The V8 engine powers Google Chrome but also Node.js which is that JavaScript Runtime.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Web/Global APIs 🌏&lt;/strong&gt;&lt;br&gt;
Web/Global APIs play a crucial role in the JavaScript Runtime Environment by enabiling the execution of tasks in the background, creating the illusion of a multi-threaded environment. It's not part of the JS Engine rather it's crucial part of Runtime environment. These APIs, provided by the browser or the operating system, allow JavaScript to interact with external resources and perform operations asynchronously.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Queue&lt;/strong&gt;&lt;br&gt;
The queue is an essential part of the JavaScript task scheduling algorithm, which dertermines the order in which tasks are executed by the JavaScript Engine.Both queues hold tasks that are executed asynchronously, but there are some differences in how they work.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Callback Queue (Macrotask Queue) ⌛&lt;/strong&gt;&lt;br&gt;
The Callback Queue also known as the task queue, holds tasks that are pushed to the queue by Web/Global APIs, such as setTimeout,setInterval,XMLHttpRequest, or Events like mouse clicks and keyboard inputs. When the call stack is empty &amp;amp; microtask queue has no priority task, the event loop moves the first task from callback queue to the call stack for execution.&lt;br&gt;
For example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

console.log('script step-1');
setTimeout(() =&amp;gt; console.log('script step-2'), 0);
console.log('script step-3');


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;4. Microtask Queue 🔬&lt;/strong&gt;&lt;br&gt;
The Microtask Queue, also known as the Job Queue or Promise Queue. It's handle the mutation observer callbacks and promise callbacks. These tasks are scheduled to run after the currently executing script is complete and before the event loop continues with the next task in the task queue.The microtask queue is processed before the callback queue, so the tasks in the microtask queue are given priority over tasks in the callback queue.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

console.log('script start');

setTimeout(() =&amp;gt; {
  console.log('setTimeout');
}, 0);

Promise.resolve().then(() =&amp;gt; {
  console.log('promise1');
}).then(() =&amp;gt; {
  console.log('promise2');
});

console.log('script end');


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;5. Event Loop 🔄&lt;/strong&gt;&lt;br&gt;
The event loop is a continuous process that constantly checks the call stack and the callback queue,microtask queue. Event loop takes callback functions from the callback queue,microtask queue and puts them in the call stack. So that they can be executed. Also Event Loop is essential for non-blocking concurrency model.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fazym2guy9hmsvsrh75eq.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fazym2guy9hmsvsrh75eq.jpg" alt="Event Loop"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;we'll explore the every components from JavaScript Runtime Environment in upcoming posts.&lt;/em&gt;&lt;br&gt;
For now, let's end it here.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>architecture</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>What is JavaScript ? Why use JavaScript? 🔥🚀</title>
      <dc:creator>Majedur Rahman</dc:creator>
      <pubDate>Sun, 12 Jun 2022 18:06:28 +0000</pubDate>
      <link>https://forem.com/rahmanmajeed/what-is-javascript-why-use-javascript-2ba6</link>
      <guid>https://forem.com/rahmanmajeed/what-is-javascript-why-use-javascript-2ba6</guid>
      <description>&lt;h2&gt;
  
  
  What Is JavaScript? ✨
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;JavaScript&lt;/strong&gt; is a high-level programming language. it's originally designed to add interactivity to web pages.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;(&lt;em&gt;high-level&lt;/em&gt; language means it's closer to human languages and further from machine language).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;JavaScript&lt;/strong&gt; also defined as &lt;em&gt;interpreted&lt;/em&gt;, &lt;em&gt;object-oriented&lt;/em&gt;, prototype-based, &lt;em&gt;just-in-time&lt;/em&gt; compiled, &lt;em&gt;multi-paradigm&lt;/em&gt; with &lt;em&gt;dynamic typing&lt;/em&gt; programming language. it's one of the core &amp;amp; leading technologies used in the web &lt;em&gt;&lt;a href="https://en.wikipedia.org/wiki/World_Wide_Web"&gt;(world wide web)&lt;/a&gt;&lt;/em&gt; alongside with HTML and CSS.&lt;/p&gt;

&lt;h2&gt;
  
  
  A bit of history 🗺️
&lt;/h2&gt;

&lt;p&gt;1995 in the offices of Netscape where the first version of JavaScript was born. At that moment though, Netscape's was one of the most popular web browsers.It's to support it that JavaScript was created, as a companion language to Java.&lt;br&gt;
Back then, the web was fully-static, powered by simple HTML &amp;amp; CSS. &lt;strong&gt;JS&lt;/strong&gt; brought dynamism.&lt;br&gt;
Even after the death of Navigator, it was eventually adopted by all succeeding web browsers, from Explorer to Firefox and Chrome.&lt;/p&gt;

&lt;p&gt;JavaScript was born in a hurry though, and various quirks would harm the language adoption in its early years. Still, some of its powerful features were part of it since its inception. These would define JS and allow it to grow.&lt;/p&gt;

&lt;p&gt;The next big step following the public release came with ECMA standardization, which became the “convention” behind JavaScript. It brought the language to a broader audience and allowed other potential implementors.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why do we Need JavaScript? 🚀
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;JavaScript is a crucial programming language for any web developer&lt;/strong&gt;. &lt;br&gt;
Without JavaScript, we wouldn’t have the dynamic and interactive web pages that have become the standard user experience we all know.&lt;br&gt;
Initially JavaScript comes as client-side language, which is executed in browsers.but modern JavaScript support up both front-end and back-end in web application development.&lt;/p&gt;

&lt;p&gt;In front-end, JavaScript client-side execution of the logic brings faster user experiences. With the code running directly in the browser, the need for server calls is abstracted, hence a cut in loading times. Even with the presence of a server, the fact that &lt;strong&gt;JS&lt;/strong&gt; is asynchronous means that it’s able to communicate with the server in the background without interrupting the user interaction taking place in the front-end.&lt;/p&gt;

&lt;p&gt;When it's used for back-end, the Node.js framework allows a server to handle front-end data updates and build scalable network applications to process simultaneous user requests.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--lQDbc8A2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j7g1wx72jd11ekmvvwpx.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--lQDbc8A2--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j7g1wx72jd11ekmvvwpx.jpg" alt="JavaScript Everywhere" width="500" height="381"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Besides front-end &amp;amp; back-end in web, we need &lt;strong&gt;JavaScript&lt;/strong&gt; in mobile app, desktop app development.🔥🔥🔥&lt;/p&gt;

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