<?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: Deveshwar Jaiswal</title>
    <description>The latest articles on DEV Community by Deveshwar Jaiswal (@svssdeva).</description>
    <link>https://dev.to/svssdeva</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%2F3876447%2F85e102c9-1eea-49ce-9402-57e394a5170a.jpeg</url>
      <title>DEV Community: Deveshwar Jaiswal</title>
      <link>https://dev.to/svssdeva</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/svssdeva"/>
    <language>en</language>
    <item>
      <title>The JS Event Loop Has a Model Gap, Here's What Most Tutorials Don't Show You</title>
      <dc:creator>Deveshwar Jaiswal</dc:creator>
      <pubDate>Mon, 13 Apr 2026 12:45:36 +0000</pubDate>
      <link>https://dev.to/svssdeva/the-js-event-loop-has-a-model-gap-heres-what-most-tutorials-dont-show-you-2pjn</link>
      <guid>https://dev.to/svssdeva/the-js-event-loop-has-a-model-gap-heres-what-most-tutorials-dont-show-you-2pjn</guid>
      <description>&lt;p&gt;The standard explanation of the JavaScript Event Loop goes like this: there's a call stack,&lt;br&gt;
there's a queue, and when the stack is empty the loop pulls the next item from the queue.&lt;/p&gt;

&lt;p&gt;That model isn't wrong. It's incomplete in ways that will eventually burn you.&lt;/p&gt;


&lt;h2&gt;
  
  
  What the standard explanation misses
&lt;/h2&gt;
&lt;h3&gt;
  
  
  1. The microtask queue is not part of the loop cycle in the way the task queue is
&lt;/h3&gt;

&lt;p&gt;Most diagrams show two queues with different priorities. The actual model is different.&lt;/p&gt;

&lt;p&gt;After every task completes, the runtime runs the &lt;strong&gt;microtask checkpoint&lt;/strong&gt;. This checkpoint&lt;br&gt;
drains the microtask queue completely — including any microtasks queued by microtasks — before&lt;br&gt;
the event loop selects the next task.&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="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="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;task&lt;/span&gt;&lt;span class="dl"&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="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&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="s1"&gt;microtask 1&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="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&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;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;microtask 2&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="s1"&gt;sync&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;Output: &lt;code&gt;sync&lt;/code&gt; → &lt;code&gt;microtask 1&lt;/code&gt; → &lt;code&gt;microtask 2&lt;/code&gt; → &lt;code&gt;task&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The setTimeout callback doesn't run until every microtask — including chains — has settled.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. The render step sits between tasks, not between microtasks
&lt;/h3&gt;

&lt;p&gt;The browser's render pipeline (style recalc, layout, paint) runs between tasks. Not between&lt;br&gt;
microtasks.&lt;/p&gt;

&lt;p&gt;This means: a long Promise chain is as harmful to your frame rate as a long synchronous&lt;br&gt;
function. No heavy computation required. If you're queueing work in Promises and wondering&lt;br&gt;
why your animations stutter, this is likely the reason.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. &lt;code&gt;setTimeout(fn, 0)&lt;/code&gt; and &lt;code&gt;Promise.resolve().then(fn)&lt;/code&gt; are different lanes
&lt;/h3&gt;

&lt;p&gt;Not different speeds. Different lanes with different rules.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;setTimeout&lt;/code&gt; → task queue. One per loop iteration. Render can happen before the next one.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Promise.resolve().then&lt;/code&gt; → microtask queue. Entire chain runs before the loop advances.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Swapping one for the other is not a performance micro-optimisation. It changes execution&lt;br&gt;
semantics.&lt;/p&gt;




&lt;h2&gt;
  
  
  The mental model I use
&lt;/h2&gt;

&lt;p&gt;I mapped this to Vedic karma and dharma — unconventional framing, but it makes the model stick.&lt;/p&gt;

&lt;p&gt;Every async operation you schedule is &lt;strong&gt;karma&lt;/strong&gt;: a consequence set in motion, deferred but&lt;br&gt;
not cancelled. The event loop is &lt;strong&gt;dharma&lt;/strong&gt;: the rule governing when consequences are processed.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Call stack&lt;/strong&gt; = the present moment. One thing at a time.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Microtask queue&lt;/strong&gt; = urgent karma. Drains immediately after the present resolves.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Task queue&lt;/strong&gt; = future karma. Waits for a full loop cycle.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Render step&lt;/strong&gt; = the breath between cycles. Can be held by urgent karma flooding.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;It's a mnemonic layer, not a replacement for reading the WHATWG HTML spec. But it's why&lt;br&gt;
the model stuck for me in a way no diagram had managed.&lt;/p&gt;




&lt;h2&gt;
  
  
  Interactive visualizer
&lt;/h2&gt;

&lt;p&gt;I built a step-by-step visualizer: write any snippet, step through execution, and watch&lt;br&gt;
every queue animate in real time. Microtask drain, render step timing, task queue behaviour&lt;br&gt;
— all visible.&lt;/p&gt;

&lt;p&gt;👉 &lt;strong&gt;&lt;a href="https://beyondcodekarma.in/javascript/js-event-loop/" rel="noopener noreferrer"&gt;Try the interactive visualizer →&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;If you're building production frontend and want to dig into performance or architecture →&lt;br&gt;
&lt;a href="https://beyondcodekarma.in/hire/" rel="noopener noreferrer"&gt;I'm available for hire&lt;/a&gt;.&lt;/p&gt;

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