<?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: Shafiq Ur Rehman</title>
    <description>The latest articles on DEV Community by Shafiq Ur Rehman (@im-shafiqurehman).</description>
    <link>https://dev.to/im-shafiqurehman</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%2F2818455%2Ffe66a016-1e91-4ff2-931d-f2f9a9fc110e.png</url>
      <title>DEV Community: Shafiq Ur Rehman</title>
      <link>https://dev.to/im-shafiqurehman</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/im-shafiqurehman"/>
    <language>en</language>
    <item>
      <title>How React Achieves High Performance, Even With Extra Layers</title>
      <dc:creator>Shafiq Ur Rehman</dc:creator>
      <pubDate>Sun, 21 Sep 2025 14:10:41 +0000</pubDate>
      <link>https://dev.to/im-shafiqurehman/how-react-achieves-high-performance-even-with-extra-layers-339j</link>
      <guid>https://dev.to/im-shafiqurehman/how-react-achieves-high-performance-even-with-extra-layers-339j</guid>
      <description>&lt;p&gt;A common interview question around React is:&lt;br&gt;
&lt;strong&gt;“If DOM updates are already costly, and React adds Virtual DOM + Reconciliation as extra steps, how can it be faster?”&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Many developers, including myself at one point, confidently answer: &lt;em&gt;“Because of Virtual DOM!”&lt;/em&gt;&lt;br&gt;
But that’s not the full picture. Let’s break it down properly.&lt;/p&gt;
&lt;h2&gt;
  
  
  First: How Browser Rendering Works (Brief Overview)
&lt;/h2&gt;

&lt;p&gt;Before we talk about React’s optimizations, let’s first understand how the browser renders things by default.&lt;/p&gt;

&lt;p&gt;When the browser receives HTML and CSS from the server, it:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Creates the &lt;strong&gt;DOM tree&lt;/strong&gt; and &lt;strong&gt;CSSOM tree&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Combines them into the &lt;strong&gt;Render Tree&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Decides the &lt;strong&gt;layout&lt;/strong&gt;, which element goes where&lt;/li&gt;
&lt;li&gt;Finally, &lt;strong&gt;paints&lt;/strong&gt; the pixels on screen&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;When something changes, like text content or styles, the DOM and CSSOM are rebuilt, the Render Tree is recreated, and then comes the expensive part: &lt;strong&gt;Reflow&lt;/strong&gt; and &lt;strong&gt;Repaint&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In this process:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Positions are recalculated&lt;/li&gt;
&lt;li&gt;Elements are repainted wherever styles or content have changed&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both Reflow and Repaint are costly operations, and this is exactly where React tries to help.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2F265b33lv87kgaf71adz7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F265b33lv87kgaf71adz7.png" alt=" " width="800" height="290"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let’s move to React and the Virtual DOM.&lt;/p&gt;
&lt;h2&gt;
  
  
  What Is Virtual DOM?
&lt;/h2&gt;

&lt;p&gt;Virtual DOM is a lightweight copy of the actual DOM, represented as a JavaScript object.&lt;/p&gt;

&lt;p&gt;Why was it needed? What was the problem with direct DOM manipulation?&lt;/p&gt;

&lt;p&gt;Let’s look at an example.&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;// Normal DOM manipulation, NOT React&lt;/span&gt;
&lt;span class="nf"&gt;setInterval&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;span&lt;/span&gt; &lt;span class="o"&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;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;span&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;span&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;textContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toLocaleTimeString&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="nf"&gt;appendChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;span&lt;/span&gt;&lt;span class="p"&gt;);&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, only the &lt;code&gt;span&lt;/code&gt;’s time is updating. But if you inspect in DevTools, you’ll see the entire &lt;code&gt;div&lt;/code&gt; re-rendering.&lt;/p&gt;

&lt;p&gt;Modern browsers are smart; if other elements existed, they wouldn’t repaint them. But even then, in this case, the browser isn’t precise enough. The entire element container is being marked for update.&lt;/p&gt;

&lt;p&gt;Now look at the same code in React:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&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;time&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setTime&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="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toLocaleTimeString&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;interval&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;setInterval&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;setTime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;toLocaleTimeString&lt;/span&gt;&lt;span class="p"&gt;());&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="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;clearInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;interval&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h3&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Current Time:&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h3&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;span&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;time&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;span&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&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;Here, only the &lt;code&gt;span&lt;/code&gt; updates. The &lt;code&gt;div&lt;/code&gt; doesn’t re-render. React has already optimized the process at this level.&lt;/p&gt;

&lt;p&gt;So how does React do this?&lt;/p&gt;

&lt;p&gt;React creates a Virtual DOM.&lt;/p&gt;

&lt;p&gt;First, it creates the initial Virtual DOM, a JS object tree mirroring your UI.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div
├── h3
├── form
│   └── input
└── span → "10:30 AM"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When state updates, say, time changes to “10:31 AM,”  React creates a new Virtual DOM tree:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div
├── h3
├── form
│   └── input
└── span → "10:31 AM"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then React compares the old and new Virtual DOM trees. This comparison process is called &lt;strong&gt;Diffing&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;React sees: “Only the text inside &lt;code&gt;span&lt;/code&gt; changed.” So it updates only that &lt;code&gt;span&lt;/code&gt; in the Real DOM, and triggers repaint for just that node.&lt;/p&gt;

&lt;p&gt;This comparison algorithm is called the &lt;strong&gt;Diffing Algorithm&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Two Key Optimizations in Diffing
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Batching Updates&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;If multiple state updates happen, React doesn’t go update the DOM each time. It batches them together and applies them in one go.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nf"&gt;setCount&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Alice&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;setLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;React will wait, collect all three, compute a final Virtual DOM, diff it, and update the Real DOM once.&lt;/p&gt;

&lt;p&gt;This avoids multiple reflows/repaints.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Element Type Comparison&lt;/strong&gt;
Let’s say you have a login/logout UI:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;isLoggedIn&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isLoggedIn&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;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Welcome back, user!&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&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;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Log In&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&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;Initial Virtual DOM (logged out):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div → class="app"
└── button → "Log In"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Updated Virtual DOM (logged in):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;div → class="app"
└── h1 → "Welcome back, user!"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;React starts comparing from the root.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;div&lt;/code&gt; → same → check props → &lt;code&gt;class="app"&lt;/code&gt; → same → move on
&lt;/li&gt;
&lt;li&gt;Now children: &lt;code&gt;button&lt;/code&gt; vs &lt;code&gt;h1&lt;/code&gt; → TYPE MISMATCH&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;React doesn’t try to “update” the button into an h1. It destroys the entire subtree and recreates it from scratch.&lt;/p&gt;

&lt;p&gt;This is efficient because trying to morph one element into another is more expensive than just replacing it.&lt;/p&gt;

&lt;p&gt;So far, this process seems optimized. Then why did React introduce Fiber?&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Was Fiber Needed?
&lt;/h2&gt;

&lt;p&gt;The original Reconciliation process had a critical flaw: &lt;strong&gt;It was synchronous and recursive&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Once started, it would run to completion, blocking the main thread.&lt;/p&gt;

&lt;p&gt;Imagine this scenario:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;User is typing in an input field
&lt;/li&gt;
&lt;li&gt;Meanwhile, 10 API calls return and trigger UI updates&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Because React’s diffing was synchronous, it would process all 10 updates in one blocking pass, freezing the UI while the user is typing.&lt;/p&gt;

&lt;p&gt;React had no way to say: “This user input is high priority, do it first. Those API updates? Do them later.”&lt;/p&gt;

&lt;p&gt;Everything was treated equally and executed in one uninterrupted stack.&lt;/p&gt;

&lt;p&gt;This hurt user experience.&lt;/p&gt;

&lt;p&gt;So in React 16, Fiber was introduced.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is React Fiber?
&lt;/h2&gt;

&lt;p&gt;React Fiber is a new Reconciliation algorithm. All updates in modern React go through Fiber.&lt;/p&gt;

&lt;p&gt;Fiber solved the core problem: &lt;strong&gt;It made Reconciliation interruptible, prioritizable, and asynchronous&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let’s understand how.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fiber Working, Step by Step
&lt;/h2&gt;

&lt;p&gt;Consider this component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&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;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setName&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="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;loading&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setLoading&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="kc"&gt;false&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="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"app"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&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;setName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Shafique&lt;/span&gt;&lt;span class="dl"&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="nf"&gt;setLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&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="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        Click to Update
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Profile&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Dashboard&lt;/span&gt; &lt;span class="na"&gt;loading&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;loading&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&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;Here:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;setName("Shafique")&lt;/code&gt; → high priority update (Sync Lane)
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;setLoading(true)&lt;/code&gt; wrapped in &lt;code&gt;startTransition&lt;/code&gt; → low priority (Transition Lane)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Fiber will handle them differently.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fiber Architecture, Key Concepts
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Fiber Node
&lt;/h3&gt;

&lt;p&gt;Every element, component, DOM node, and text becomes a &lt;strong&gt;Fiber Node&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Example component tree:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;App&amp;gt;
  ├── &amp;lt;h2&amp;gt;
  ├── &amp;lt;Profile&amp;gt;
  └── &amp;lt;Dashboard&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Becomes a Fiber Tree where each node is a unit of work.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Current Tree vs WIP(Work-In-Progress Tree)
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Current Tree&lt;/strong&gt; → The tree currently rendered on screen
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Work-In-Progress (WIP) Tree&lt;/strong&gt; → The tree being prepared for next render&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When updates happen, React builds the WIP tree and then swaps it with the Current Tree during the Commit Phase.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fiber Reconciliation Two Phases
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Phase 1: Render Phase (Interruptible)
&lt;/h3&gt;

&lt;p&gt;This phase has two sub-phases:&lt;/p&gt;

&lt;h4&gt;
  
  
  a. Begin Work
&lt;/h4&gt;

&lt;p&gt;React visits each Fiber Node starting from the root.&lt;/p&gt;

&lt;p&gt;It checks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Does this node need update?
&lt;/li&gt;
&lt;li&gt;What’s the new state/props?
&lt;/li&gt;
&lt;li&gt;Create/clone Fiber Node for WIP tree&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  b. Complete Work
&lt;/h4&gt;

&lt;p&gt;After a node’s children are processed, React:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Creates the actual DOM node (if new)
&lt;/li&gt;
&lt;li&gt;Links it to the Fiber Node via &lt;code&gt;stateNode&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Adds the Fiber Node to the “Effect List”  if it needs a DOM update&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&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="nx"&gt;fiber&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stateNode&lt;/span&gt; &lt;span class="o"&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;createElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;h2&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.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%2F788c8ce8fps0pl4wjtmo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2F788c8ce8fps0pl4wjtmo.png" alt=" " width="800" height="459"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Effect List is a linked list of nodes that need DOM mutations.&lt;/p&gt;

&lt;h2&gt;
  
  
  Traversal Order, Depth First
&lt;/h2&gt;

&lt;p&gt;Fiber doesn’t use recursion; it uses a linked list with pointers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;child&lt;/code&gt; → first child
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;sibling&lt;/code&gt; → next sibling
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;return&lt;/code&gt; → parent&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Traversal order:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Start at Root
&lt;/li&gt;
&lt;li&gt;Go to the child
&lt;/li&gt;
&lt;li&gt;Keep going to the child until the leaf
&lt;/li&gt;
&lt;li&gt;At leaf → go to sibling
&lt;/li&gt;
&lt;li&gt;If no sibling → go back to parent
&lt;/li&gt;
&lt;li&gt;Parents’ sibling? Go there. Otherwise, go to the grandparent.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Root
└── App
    ├── h2
    ├── Profile
    └── Dashboard
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Traversal:&lt;/p&gt;

&lt;p&gt;Root → App → h2 (leaf) → Profile (sibling) → Dashboard (sibling) → App (parent) → Root&lt;/p&gt;

&lt;p&gt;At each node, Begin Work → then, after children → Complete Work.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fjdwfy7ilp4jmtnxmocio.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fjdwfy7ilp4jmtnxmocio.png" alt=" " width="720" height="540"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Phase 2: Commit Phase (Synchronous)
&lt;/h2&gt;

&lt;p&gt;Once the Render Phase is done, React has:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A complete WIP Fiber Tree
&lt;/li&gt;
&lt;li&gt;An Effect List with all nodes needing DOM updates&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, React enters the Commit Phase, which is &lt;strong&gt;synchronous and uninterruptible&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;It walks the Effect List and performs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Insertions
&lt;/li&gt;
&lt;li&gt;Updates
&lt;/li&gt;
&lt;li&gt;Deletions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On the Real DOM.&lt;/p&gt;

&lt;p&gt;Then, it swaps WIP Tree → becomes new Current Tree.&lt;/p&gt;

&lt;h2&gt;
  
  
  Update Phase, How Priorities Work
&lt;/h2&gt;

&lt;p&gt;When state updates:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;React creates an &lt;strong&gt;Update Object&lt;/strong&gt; → { payload, timestamp, lane }
&lt;/li&gt;
&lt;li&gt;Enqueues it in the component’s update queue
&lt;/li&gt;
&lt;li&gt;Marks the Fiber Node (and all ancestors) as “needing work”
&lt;/li&gt;
&lt;li&gt;Schedules the update based on lane (priority)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Example:&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;// High priority&lt;/span&gt;
&lt;span class="nf"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Shafique&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Sync Lane&lt;/span&gt;

&lt;span class="c1"&gt;// Low priority&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;setLoading&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Transition Lane&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;React’s Scheduler:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Checks which updates are pending
&lt;/li&gt;
&lt;li&gt;Assigns priority: Sync, Transition, Idle
&lt;/li&gt;
&lt;li&gt;Executes high-priority first&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So when you click the button:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;React creates a WIP(work-in-progress) tree
&lt;/li&gt;
&lt;li&gt;Processes &lt;code&gt;setName("Shafique")&lt;/code&gt; → updates Profile
&lt;/li&gt;
&lt;li&gt;Skips &lt;code&gt;setLoading(true)&lt;/code&gt; for now (low priority)
&lt;/li&gt;
&lt;li&gt;Commits → UI updates immediately
&lt;/li&gt;
&lt;li&gt;Later — starts new WIP tree → processes &lt;code&gt;setLoading(true)&lt;/code&gt; → commits Dashboard update&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The user sees instant feedback, and background work occurs later.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fiber’s Real Power
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Work is split into chunks → doesn’t block the main thread
&lt;/li&gt;
&lt;li&gt;High-priority work (user input) jumps the queue
&lt;/li&gt;
&lt;li&gt;Low priority work (data loading) waits — but doesn’t block
&lt;/li&gt;
&lt;li&gt;Browser gets breathing room → stays responsive&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Even though Fiber adds more steps, it makes the right steps happen at the right time.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>react</category>
      <category>discuss</category>
      <category>frontend</category>
    </item>
    <item>
      <title>How Node.js Achieves High Performance &amp; Scalability</title>
      <dc:creator>Shafiq Ur Rehman</dc:creator>
      <pubDate>Sat, 20 Sep 2025 06:25:36 +0000</pubDate>
      <link>https://dev.to/im-shafiqurehman/how-nodejs-achieves-high-performance-scalability-3lad</link>
      <guid>https://dev.to/im-shafiqurehman/how-nodejs-achieves-high-performance-scalability-3lad</guid>
      <description>&lt;h2&gt;
  
  
  What is Node.js?
&lt;/h2&gt;

&lt;p&gt;Node.js is an open-source JavaScript runtime environment that allows you to develop scalable web applications (accessible on the internet, without requiring installation on the user's device). This environment is built on top of Google Chrome’s JavaScript Engine V8. It uses an event-driven(waits for things to happen and then reacts to them), non-blocking I/O model(sends I/O requests and continuously does other work, notified when done), making it lightweight, more efficient, and perfect for data-intensive real-time applications running across shared devices.&lt;/p&gt;

&lt;h2&gt;
  
  
  Non-Blocking I/O: The Performance Game-Changer
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Runs on the &lt;strong&gt;side stack&lt;/strong&gt; (callback queue/microtask queue).&lt;/li&gt;
&lt;li&gt;The main thread &lt;strong&gt;does not wait&lt;/strong&gt;, async operations run in the background, and their callbacks execute later.&lt;/li&gt;
&lt;li&gt;Example:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fs = require('fs');
fs.readFile('file.txt', (err, data) =&amp;gt; {     // Non-blocking
console.log(”This runs after the file reading is completes” , data);
});
console.log("This runs immediately");

 Here, console.log("This runs immediately") executes first, and the file reading happens in the background.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Node.js Architecture Overview
&lt;/h2&gt;

&lt;p&gt;This architecture is mainly based on 5 key components:&lt;/p&gt;

&lt;p&gt;1️⃣ &lt;strong&gt;Single Thread&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;2️⃣ &lt;strong&gt;Event Loop&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;3️⃣ &lt;strong&gt;Event Queue&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;4️⃣ &lt;strong&gt;Worker Pool (Libuv)&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;5️⃣ &lt;strong&gt;V8 Engine&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Single Thread&lt;/strong&gt;&lt;br&gt;
Node.js operates in a single-threaded environment. This means:&lt;/p&gt;

&lt;p&gt;Only one thread executes JavaScript code.&lt;/p&gt;

&lt;p&gt;This thread handles the main event loop.&lt;/p&gt;

&lt;p&gt;This is why Node.js is lightweight.&lt;/p&gt;

&lt;p&gt;In simple terms, a single thread handles requests from multiple users, resulting in low memory usage.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Event Loop: Node.js’s Secret Weapon&lt;/strong&gt;&lt;br&gt;
The event loop runs indefinitely and connects the call stack, the microtask queue, and the callback queue. The event loop moves asynchronous tasks from the microtask queue and the callback queue to the call stack whenever the call stack is empty.&lt;/p&gt;

&lt;p&gt;Callback Queue:&lt;br&gt;
 Callback functions for operations like setTimeout() are added here before moving to the call stack.&lt;/p&gt;

&lt;p&gt;Microtask Queue: &lt;br&gt;
Callback functions for Promises and MutationObserver are queued here and have higher priority.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Event Queue&lt;/strong&gt;&lt;br&gt;
When asynchronous operations (like HTTP requests, database queries) are performed:&lt;/p&gt;

&lt;p&gt;Node.js places them in the event queue.&lt;/p&gt;

&lt;p&gt;The event loop then processes this queue when the main thread is free.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Offloading Heavy Work: Libuv &amp;amp; Worker Pool&lt;/strong&gt;&lt;br&gt;
Node.js is single-threaded, but that doesn’t mean it can’t do parallel work.&lt;/p&gt;

&lt;p&gt;For blocking I/O tasks (file system, DNS, crypto, compression), Node.js uses Libuv’s Worker Pool, a pool of 4 background threads (configurable) that handle heavy lifting.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Why This Matters for Performance:
Your main thread stays free to handle new requests.
I/O-bound tasks run in parallel without blocking JavaScript execution.
CPU-bound tasks? Use worker_threads or offload to microservices.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In simple terms, Node.js's single thread handles the main application logic, while heavy tasks are handled in the background by the worker pool.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;V8 Engine: Raw Speed Under the Hood&lt;/strong&gt;&lt;br&gt;
Node.js runs on Google’s V8 JavaScript Engine, the same engine that powers Chrome.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Performance Benefits:
Just-In-Time Compilation: Converts JS to optimized machine code at runtime.
Dynamic Optimization: Frequently used functions get turbocharged.
Garbage Collection: Efficient memory management prevents leaks and slowdowns.
V8 is why Node.js apps start fast, run fast, and stay fast, even under heavy load. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fxazesjkkm8up1gcf1y28.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fxazesjkkm8up1gcf1y28.png" alt="A simplified flowchart illustrating the non-blocking flow of a request in Node.js: Incoming Request -&amp;gt; Event Loop -&amp;gt; Immediate processing for non-blocking tasks or delegation to the Worker Pool for heavy tasks -&amp;gt; Response." width="397" height="515"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Node.js Flow Example:
&lt;/h2&gt;

&lt;p&gt;1️⃣ A user sends an API request.&lt;/p&gt;

&lt;p&gt;2️⃣ Node.js receives the request.&lt;/p&gt;

&lt;p&gt;3️⃣ If the request involves:&lt;/p&gt;

&lt;p&gt;A non-heavy CPU task is executed directly via the event loop.&lt;/p&gt;

&lt;p&gt;A heavy task (like reading a file) is sent to the worker pool.&lt;/p&gt;

&lt;p&gt;4️⃣ While the task is processing, the event loop continues to handle other requests.&lt;/p&gt;

&lt;p&gt;5️⃣ Once the task is complete, its callback function is placed in the event queue.&lt;/p&gt;

&lt;p&gt;6️⃣ The event loop picks up the callback and executes it.&lt;/p&gt;

&lt;p&gt;7️⃣ Node.js sends the response back to the user.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fvuwrdzyd3flico2krww8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fvuwrdzyd3flico2krww8.png" alt="Detailed diagram of the Node.js runtime architecture showing the relationship between the V8 engine, the Event Loop with its call stack, and the Libuv thread pool which handles I/O operations and delegates work to worker threads." width="691" height="479"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Pro Tips for Optimizing Node.js Performance
&lt;/h2&gt;

&lt;p&gt;Never Use Sync APIs&lt;br&gt;
→ readFileSync and writeFileSync will destroy your server's throughput.&lt;/p&gt;

&lt;p&gt;Use Async/Await or Promises&lt;br&gt;
→ Less messy, faster, and easier to debug than callbacks.&lt;/p&gt;

&lt;p&gt;Cluster Your App&lt;br&gt;
→ Utilize all CPU cores using the cluster module.&lt;/p&gt;

&lt;p&gt;Offload CPU Work&lt;br&gt;
→ Leverage worker_threads for heavy computation.&lt;/p&gt;

&lt;p&gt;Use Caching &amp;amp; Streaming&lt;br&gt;
→ Minimize I/O roundtrips. Stream large files instead of loading into memory.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thought
&lt;/h2&gt;

&lt;p&gt;Node.js doesn’t achieve high performance by throwing more hardware at the problem; it does so by being intelligent with resources. Its event-driven, non-blocking model is purpose-built for modern, I/O-heavy applications.&lt;/p&gt;

&lt;p&gt;Master these concepts, avoid blocking code, and you’ll unlock Node.js’s true potential: a server that’s fast, lean, and ready to scale&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>performance</category>
      <category>javascript</category>
      <category>node</category>
    </item>
    <item>
      <title>JavaScript Execution Context Made Simple</title>
      <dc:creator>Shafiq Ur Rehman</dc:creator>
      <pubDate>Thu, 21 Aug 2025 10:19:34 +0000</pubDate>
      <link>https://dev.to/im-shafiqurehman/javascript-execution-context-made-simple-5gk0</link>
      <guid>https://dev.to/im-shafiqurehman/javascript-execution-context-made-simple-5gk0</guid>
      <description>&lt;p&gt;A JavaScript engine is a program that converts JavaScript code into a Binary Language. Computers understand the Binary Language. Every web browser contains a JavaScript engine. For example, V8 is the JavaScript engine in Google Chrome.&lt;/p&gt;

&lt;p&gt;Let's dive in!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.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%2Fdrlyovje7ykt4ef2d7v6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Fdrlyovje7ykt4ef2d7v6.png" alt="Diagram showing synchronous vs asynchronous JavaScript execution" width="800" height="800"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Execution context&lt;/strong&gt;: Execution Context is the environment in which JS code runs. It decides what variables and functions are accessible, and how the code executes. It has two types (Global &amp;amp; Function) and works in two phases (Memory Creation &amp;amp; Code Execution).&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;Global Execution Context (GEC)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This is created &lt;strong&gt;once&lt;/strong&gt; when your script starts. It's the outermost context where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Global variables and functions are stored&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;this&lt;/code&gt; refers to the global object (like &lt;code&gt;window&lt;/code&gt; in browsers)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. &lt;strong&gt;Function Execution Context (FEC)&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;When you &lt;strong&gt;call a function&lt;/strong&gt;, a &lt;strong&gt;new context&lt;/strong&gt; is created specifically for that function. It manages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The function's local variables&lt;/li&gt;
&lt;li&gt;The value of &lt;code&gt;this&lt;/code&gt; inside the function&lt;/li&gt;
&lt;li&gt;Arguments passed to the function&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. &lt;strong&gt;Memory Creation Phase&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This is the &lt;strong&gt;first phase&lt;/strong&gt; of an execution context. During this phase:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;All &lt;strong&gt;variables and functions&lt;/strong&gt; are allocated in memory&lt;/li&gt;
&lt;li&gt;Functions are &lt;strong&gt;fully hoisted&lt;/strong&gt; (stored with their complete code)&lt;/li&gt;
&lt;li&gt;Variables declared with &lt;strong&gt;&lt;code&gt;var&lt;/code&gt;&lt;/strong&gt; are hoisted and initialized with &lt;code&gt;undefined&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Variables declared with &lt;strong&gt;&lt;code&gt;let&lt;/code&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;code&gt;const&lt;/code&gt;&lt;/strong&gt; are also hoisted but remain uninitialized, staying in the &lt;strong&gt;Temporal Dead Zone (TDZ)&lt;/strong&gt; until their declaration is reached&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. &lt;strong&gt;Code Execution Phase&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;This is the &lt;strong&gt;second phase&lt;/strong&gt;, where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The code &lt;strong&gt;executes line by line&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Variables receive their actual values&lt;/li&gt;
&lt;li&gt;Functions are called when invoked&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The &lt;strong&gt;Variable Environment&lt;/strong&gt; is a &lt;strong&gt;part of the Execution Context&lt;/strong&gt;.&lt;br&gt;
It is &lt;strong&gt;where all variables, functions, and arguments are stored in memory&lt;/strong&gt; as key-value pairs during the &lt;strong&gt;Memory Creation Phase&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;It includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Variable declarations&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Function declarations&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Function parameters&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;It is &lt;strong&gt;used internally by the JS engine&lt;/strong&gt; to track what's defined in the current scope.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Call stack&lt;/strong&gt;: The call stack is a part of the JavaScript engine that helps keep track of function calls. When a function is invoked, it is pushed to the call stack, where its execution begins. When the execution is complete, the function is popped off the call stack. It utilizes the concept of stacks in data structures, following the Last-In-First-Out (LIFO) principle.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Event loop:&lt;/strong&gt; The event loop runs indefinitely and connects the call stack, the microtask queue, and the callback queue. The event loop moves asynchronous tasks from the microtask queue and the callback queue to the call stack whenever the call stack is empty.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;em&gt;In JavaScript’s event loop, microtasks always have higher priority than macrotasks (callback queue).&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Callback Queue (Macrotask Queue):   Callback functions for setTimeout() are added to the callback queue before they are moved to the call stack for execution.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;setTimeout()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;setInterval()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;setImmediate()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;I/O events&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Microtask queue:&lt;/strong&gt; Asynchronous callback functions for promises and mutation observers are queued in the microtask queue before they are moved to the call stack for execution.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Includes things like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;Promise.then()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Promise.catch()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Promise.finally()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;MutationObserver&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  &lt;strong&gt;Synchronous JavaScript&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;JavaScript is synchronous, blocking, and single-threaded. This means the JavaScript engine executes code sequentially—one line at a time from top to bottom—in the exact order of the statements.&lt;/p&gt;

&lt;p&gt;Consider a scenario with three &lt;code&gt;console.log&lt;/code&gt; statements.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&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="s2"&gt;First line&lt;/span&gt;&lt;span class="dl"&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="s2"&gt;Second line&lt;/span&gt;&lt;span class="dl"&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="s2"&gt;Third line&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nl"&gt;Output&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;span class="nx"&gt;First&lt;/span&gt; &lt;span class="nx"&gt;line&lt;/span&gt;
&lt;span class="nx"&gt;Second&lt;/span&gt; &lt;span class="nx"&gt;line&lt;/span&gt;
&lt;span class="nx"&gt;Third&lt;/span&gt; &lt;span class="nx"&gt;line&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's examine another example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;greetUser&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;userName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Shafiq Ur Rehman&lt;/span&gt;&lt;span class="dl"&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="s2"&gt;`Hello, &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;userName&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;!`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="nf"&gt;greetUser&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

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

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;A new &lt;strong&gt;global execution context&lt;/strong&gt; is created and pushed onto the call stack. This is the main execution context where the top-level code runs. Every program has only one global execution context, and it always stays at the bottom of the call stack.&lt;/li&gt;
&lt;li&gt;In the global execution context, the &lt;strong&gt;memory creation phase&lt;/strong&gt; starts. In this phase, all variables and functions declared in the program are allocated space in memory (called the variable environment). Since we don’t have variables declared in the global scope, only the functions will be stored in memory.&lt;/li&gt;
&lt;li&gt;The function &lt;code&gt;getName&lt;/code&gt; is stored in memory, with its reference pointing to the full function body. The code inside it isn’t executed yet—it will run only when the function is called.&lt;/li&gt;
&lt;li&gt;Similarly, the function &lt;code&gt;greetUser&lt;/code&gt; is stored in memory, with its reference pointing to its entire function body.&lt;/li&gt;
&lt;li&gt;When the &lt;code&gt;greetUser&lt;/code&gt; function is invoked, the code execution phase of the global execution context begins. A new execution context for &lt;code&gt;greetUser&lt;/code&gt; is created and pushed on top of the call stack. Just like any execution context, it first goes through the memory allocation phase.&lt;/li&gt;
&lt;li&gt;Inside &lt;code&gt;greetUser&lt;/code&gt;, the variable &lt;code&gt;userName&lt;/code&gt; is allocated space in memory and initialized with &lt;code&gt;undefined&lt;/code&gt;. (&lt;strong&gt;Note:&lt;/strong&gt; During memory creation, variables declared with &lt;code&gt;var&lt;/code&gt; are initialized with &lt;code&gt;undefined&lt;/code&gt;, while variables declared with &lt;code&gt;let&lt;/code&gt; and &lt;code&gt;const&lt;/code&gt; are set as &lt;em&gt;uninitialized&lt;/em&gt;, which leads to a reference error if accessed before assignment.)&lt;/li&gt;
&lt;li&gt;After the memory phase finishes, the code execution phase starts. The variable &lt;code&gt;userName&lt;/code&gt; needs the result of the &lt;code&gt;getName&lt;/code&gt; function call. So &lt;code&gt;getName&lt;/code&gt; is invoked, and a new execution context for &lt;code&gt;getName&lt;/code&gt; is pushed onto the call stack.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The function &lt;code&gt;getName&lt;/code&gt; allocates space for its parameter &lt;code&gt;name&lt;/code&gt;, initializes it with &lt;code&gt;undefined&lt;/code&gt;, and then assigns it the value &lt;code&gt;"Shafiq Ur Rehman"&lt;/code&gt;. Once the &lt;code&gt;return&lt;/code&gt; statement runs, that value is returned to the &lt;code&gt;greetUser&lt;/code&gt; context. The &lt;code&gt;getName&lt;/code&gt; execution context is then popped off the call stack. Execution goes back to &lt;code&gt;greetUser&lt;/code&gt;, where the returned value is assigned to &lt;code&gt;userName&lt;/code&gt;. Next, the &lt;code&gt;console.log&lt;/code&gt; statement runs and prints:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello, Shafiq Ur Rehman!
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;Once done, the &lt;code&gt;greetUser&lt;/code&gt; The execution context is also popped off the call stack.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finally, the program returns to the global execution context. Since there’s no more code left to run, the global context is popped off the call stack, and the program ends.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Asynchronous JavaScript&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Unlike synchronous operations, asynchronous operations don't block subsequent tasks from starting, even if the current task isn't finished. The JavaScript engine works with Web APIs (like setTimeout, setInterval, etc.) in the browser to enable asynchronous behavior.&lt;/p&gt;

&lt;p&gt;Using Web APIs, JavaScript offloads time-consuming tasks to the browser while continuing to execute synchronous operations. This asynchronous approach allows tasks that take time (like database access or file operations) to run in the background without blocking the execution of subsequent code.&lt;/p&gt;

&lt;p&gt;Let’s break this down with a &lt;code&gt;setTimeout()&lt;/code&gt; example. (I’ll skip memory allocation here since we already covered it earlier.)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&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="s2"&gt;first&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&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="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="s2"&gt;second&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="mi"&gt;3000&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="s2"&gt;third&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Here’s what happens when this code runs:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The program starts with a &lt;strong&gt;global execution context&lt;/strong&gt; created and pushed onto the call stack.&lt;/li&gt;
&lt;li&gt;The first line &lt;code&gt;console.log("first")&lt;/code&gt; runs. It creates an execution context, prints &lt;code&gt;"first"&lt;/code&gt; to the console, and then is popped off the stack.&lt;/li&gt;
&lt;li&gt;Next, the &lt;code&gt;setTimeout()&lt;/code&gt; function is called. Since it’s a &lt;strong&gt;Web API provided by the browser&lt;/strong&gt;, it doesn’t run fully inside the call stack. Instead, it takes two arguments: a callback function and a delay (3000ms here). The browser registers the callback function in the Web API environment, starts a timer for 3 seconds, and then &lt;code&gt;setTimeout()&lt;/code&gt; itself is popped off the stack.&lt;/li&gt;
&lt;li&gt;Execution moves on to &lt;code&gt;console.log("third")&lt;/code&gt;. This prints &lt;code&gt;"third"&lt;/code&gt; immediately, and that context is also popped off.&lt;/li&gt;
&lt;li&gt;Meanwhile, the callback function from &lt;code&gt;setTimeout&lt;/code&gt; is sitting in the Web API environment, waiting for the 3-second timer to finish.&lt;/li&gt;
&lt;li&gt;Once the timer completes, the callback doesn’t go straight to the call stack. Instead, it’s placed into the &lt;strong&gt;callback queue&lt;/strong&gt;. This queue only runs when the call stack is completely clear. So even if you had thousands of lines of synchronous code after &lt;code&gt;setTimeout&lt;/code&gt;, they would all finish first.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;event loop&lt;/strong&gt; is the mechanism that keeps watching the call stack and the queues. When the call stack is empty, the event loop takes the callback from the queue and pushes it onto the stack.&lt;/li&gt;
&lt;li&gt;Finally, the callback runs: &lt;code&gt;console.log("second")&lt;/code&gt; prints &lt;code&gt;"second"&lt;/code&gt; to the console. After that, the callback function itself is popped off, and eventually, the global execution context is cleared once everything has finished.&lt;/li&gt;
&lt;/ol&gt;

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

&lt;p&gt;JavaScript runs code synchronously but can handle async tasks using browser Web APIs. Knowing how the engine works under the hood is key to mastering the language.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;“Let me know your thoughts in the comments,”&lt;/em&gt; or &lt;em&gt;“Follow me for more JavaScript insights.”&lt;/em&gt;&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
