<?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: Rahul Vijayvergiya</title>
    <description>The latest articles on DEV Community by Rahul Vijayvergiya (@rahulvijayvergiya).</description>
    <link>https://dev.to/rahulvijayvergiya</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%2F1613510%2F975e22e5-c887-4a62-a6b7-74b2e60ba1de.jpeg</url>
      <title>DEV Community: Rahul Vijayvergiya</title>
      <link>https://dev.to/rahulvijayvergiya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rahulvijayvergiya"/>
    <language>en</language>
    <item>
      <title>Load Balancer vs API Gateway (can one replace other)</title>
      <dc:creator>Rahul Vijayvergiya</dc:creator>
      <pubDate>Wed, 17 Dec 2025 09:45:02 +0000</pubDate>
      <link>https://dev.to/rahulvijayvergiya/load-balancer-vs-api-gateway-can-one-replace-other-3271</link>
      <guid>https://dev.to/rahulvijayvergiya/load-balancer-vs-api-gateway-can-one-replace-other-3271</guid>
      <description>&lt;p&gt;This post was initially published on &lt;a href="https://rahulvijayvergiya.hashnode.dev/" rel="noopener noreferrer"&gt;my blog&lt;/a&gt;. Check out the original source using the link below:&lt;br&gt;


&lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
    &lt;div class="c-embed__content"&gt;
      &lt;div class="c-embed__body flex items-center justify-between"&gt;
        &lt;a href="https://rahulvijayvergiya.hashnode.dev/load-balancer-vs-api-gateway-can-one-replace-other" rel="noopener noreferrer" class="c-link fw-bold flex items-center"&gt;
          &lt;span class="mr-2"&gt;rahulvijayvergiya.hashnode.dev&lt;/span&gt;
          

        &lt;/a&gt;
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;




&lt;p&gt;In modern architectures, &lt;strong&gt;Load Balancers&lt;/strong&gt; and &lt;strong&gt;API Gateways&lt;/strong&gt; are both critical components, but they solve &lt;strong&gt;different problems&lt;/strong&gt;.&lt;br&gt;&lt;br&gt;
Because they sometimes appear in the same request path, they are often confused or even treated as interchangeable.&lt;/p&gt;

&lt;h3&gt;
  
  
  Load Balancer and OSI Layers
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Load Balancer Type&lt;/th&gt;
&lt;th&gt;OSI Layer&lt;/th&gt;
&lt;th&gt;What It Understands&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;L4 Load Balancer&lt;/td&gt;
&lt;td&gt;Layer 4 (Transport)&lt;/td&gt;
&lt;td&gt;TCP/UDP, IP, Port&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;L7 Load Balancer&lt;/td&gt;
&lt;td&gt;Layer 7 (Application)&lt;/td&gt;
&lt;td&gt;HTTP/HTTPS, headers, paths&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Key point:&lt;/strong&gt;&lt;br&gt;&lt;br&gt;
A Load Balancer primarily focuses on &lt;strong&gt;traffic distribution&lt;/strong&gt;, not API logic.&lt;/p&gt;

&lt;h3&gt;
  
  
  API Gateway and OSI Layers
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Component&lt;/th&gt;
&lt;th&gt;OSI Layer&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;API Gateway&lt;/td&gt;
&lt;td&gt;Layer 7 (Application)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;An API Gateway &lt;strong&gt;deeply understands HTTP semantics&lt;/strong&gt;, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Headers&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;JSON payloads&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Authorization tokens&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Request paths&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;API versions&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  4. Key Differences at a Glance
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Aspect&lt;/th&gt;
&lt;th&gt;Load Balancer&lt;/th&gt;
&lt;th&gt;API Gateway&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Primary Purpose&lt;/td&gt;
&lt;td&gt;Distribute traffic&lt;/td&gt;
&lt;td&gt;Manage APIs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;OSI Layer&lt;/td&gt;
&lt;td&gt;L4 or L7&lt;/td&gt;
&lt;td&gt;L7 only&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Authentication&lt;/td&gt;
&lt;td&gt;❌ (Very limited)&lt;/td&gt;
&lt;td&gt;✅ Built-in&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Rate Limiting&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;API Versioning&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Request Transformation&lt;/td&gt;
&lt;td&gt;❌&lt;/td&gt;
&lt;td&gt;✅&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Protocol Awareness&lt;/td&gt;
&lt;td&gt;TCP/HTTP&lt;/td&gt;
&lt;td&gt;HTTP/REST/GraphQL&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Backend Awareness&lt;/td&gt;
&lt;td&gt;Servers&lt;/td&gt;
&lt;td&gt;Microservices &amp;amp; APIs&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Because both can work at &lt;strong&gt;Layer 7 (Application Layer)&lt;/strong&gt;, a common question comes up:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;“If both understand HTTP, why can’t one replace the other?”&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  What Is a Load Balancer?
&lt;/h2&gt;

&lt;p&gt;A &lt;strong&gt;Load Balancer&lt;/strong&gt; is like a &lt;strong&gt;traffic police officer&lt;/strong&gt; 🚦.&lt;/p&gt;

&lt;h3&gt;
  
  
  Its main job:
&lt;/h3&gt;

&lt;p&gt;👉 &lt;strong&gt;Decide which server should handle the request&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Layer 4 = Transport Layer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It deals with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;IP address&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Port number&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;TCP / UDP&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Connections&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At this layer, the load balancer &lt;strong&gt;does NOT understand HTTP&lt;/strong&gt;, URLs, or headers.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Layer 7 = Application Layer&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Even when working at &lt;strong&gt;Layer 7&lt;/strong&gt;, a load balancer mainly does:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Distributes traffic across servers&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Path-based routing (&lt;code&gt;/api → server A&lt;/code&gt;)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Sticky sessions (same user → same server)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Health checks&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;SSL termination&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;NOTE:&lt;/strong&gt; Sticky sessions only mean:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;“Send this user to the same server again”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It does &lt;strong&gt;not&lt;/strong&gt; mean: Checking identity, Validating tokens, Enforcing permissions&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is an API Gateway?
&lt;/h2&gt;

&lt;p&gt;An &lt;strong&gt;API Gateway&lt;/strong&gt; is like a &lt;strong&gt;security guard at the building entrance&lt;/strong&gt; 🛂.&lt;/p&gt;

&lt;h3&gt;
  
  
  Its main job:
&lt;/h3&gt;

&lt;p&gt;👉 &lt;strong&gt;Control access to APIs&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It handles: Authentication (JWT, OAuth, API keys), Authorization (what user can do), Rate limiting &amp;amp; throttling, API versioning (&lt;code&gt;v1&lt;/code&gt;, &lt;code&gt;v2&lt;/code&gt;), Request / response transformation, Logging and monitoring&lt;/p&gt;

&lt;p&gt;API Gateway cares about:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Who is calling, how often, and what they are allowed to access&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Why Load Balancer Cannot Handle Authentication
&lt;/h2&gt;

&lt;p&gt;Even though a load balancer can &lt;strong&gt;read headers&lt;/strong&gt;, authentication is more than reading headers.&lt;/p&gt;

&lt;p&gt;Authentication involves: Token validation, Expiry checks, Role &amp;amp; permission checks, Sometimes DB or IAM calls&lt;/p&gt;

&lt;p&gt;Load balancers are designed to be:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Fast, lightweight, and simple&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If they start doing heavy security logic then Performance will drop, Scaling becomes harder&lt;/p&gt;

&lt;p&gt;That’s why &lt;strong&gt;authentication belongs to API Gateway&lt;/strong&gt;, not Load Balancer.&lt;/p&gt;

&lt;h2&gt;
  
  
  Can One Replace the Other?
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Can API Gateway replace Load Balancer?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Sometimes&lt;/strong&gt;, in small or serverless setups&lt;/p&gt;

&lt;h3&gt;
  
  
  Can Load Balancer replace API Gateway?
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;No&lt;/strong&gt;, because it does not manage API security, limits, or versions&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Summary
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Load Balancer&lt;/strong&gt; answers: &lt;em&gt;“Where should this request go?”&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;API Gateway&lt;/strong&gt; answers: &lt;em&gt;“Who is calling and what can they do?”&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>node</category>
      <category>react</category>
    </item>
    <item>
      <title>Sundar, Satya and Sam's AI Dream: Take everyones job</title>
      <dc:creator>Rahul Vijayvergiya</dc:creator>
      <pubDate>Wed, 11 Jun 2025 21:33:54 +0000</pubDate>
      <link>https://dev.to/rahulvijayvergiya/sundar-satya-and-sam-ai-dream-take-everyones-job-5c1d</link>
      <guid>https://dev.to/rahulvijayvergiya/sundar-satya-and-sam-ai-dream-take-everyones-job-5c1d</guid>
      <description>&lt;p&gt;This post was initially published on &lt;a href="https://rahulvijayvergiya.hashnode.dev/sundar-satya-and-sams-ai-dream-take-everyones-job" rel="noopener noreferrer"&gt;my blog&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;em&gt;Why AI Doesn’t Take Your Job — and Why That Worries Them More Than You&lt;/em&gt;
&lt;/h2&gt;

&lt;p&gt;Sundar, Satya, and Sam &lt;strong&gt;promise&lt;/strong&gt; a future where AI transforms everything — from your job to your brain. They &lt;strong&gt;stand&lt;/strong&gt; on stages, &lt;strong&gt;preach&lt;/strong&gt; about the AI revolution, and &lt;strong&gt;declare&lt;/strong&gt; this is the moment we all wait for. But behind the billion-dollar valuations and press releases, they &lt;strong&gt;struggle&lt;/strong&gt; to keep the illusion alive.&lt;/p&gt;

&lt;p&gt;They &lt;strong&gt;promise&lt;/strong&gt; AI will replace workers. But it doesn’t. It &lt;strong&gt;struggles&lt;/strong&gt; to write a coherent paragraph, &lt;strong&gt;hallucinates&lt;/strong&gt; facts, and &lt;strong&gt;needs&lt;/strong&gt; more human babysitting than a toddler with a Sharpie.&lt;/p&gt;

&lt;p&gt;Let’s be honest — &lt;strong&gt;AI doesn’t take your job&lt;/strong&gt;. It &lt;strong&gt;wants&lt;/strong&gt; your time, your data, and your belief. But it can’t truly &lt;strong&gt;replace&lt;/strong&gt; you. Not because it lacks processing power — but because it &lt;strong&gt;lacks soul&lt;/strong&gt;. And that’s something not even the best models can mimic.&lt;/p&gt;




&lt;h3&gt;
  
  
  🤖 The Great Job Panic: Manufactured, Marketed, and Failing
&lt;/h3&gt;

&lt;p&gt;Sundar, Satya, and Sam &lt;strong&gt;help fuel&lt;/strong&gt; a global panic — that AI &lt;strong&gt;comes&lt;/strong&gt; for your job. But most AI tools today &lt;strong&gt;can’t even summarise a PDF correctly&lt;/strong&gt;, let alone &lt;strong&gt;run&lt;/strong&gt; your business. It &lt;strong&gt;generates&lt;/strong&gt; code that breaks, &lt;strong&gt;writes&lt;/strong&gt; text with no voice, and &lt;strong&gt;fails&lt;/strong&gt; at nuance.&lt;/p&gt;

&lt;p&gt;The real danger isn’t AI itself — it’s &lt;strong&gt;executives who believe the pitch decks&lt;/strong&gt; and &lt;strong&gt;fire human talent&lt;/strong&gt; to gamble on incomplete tech that often &lt;strong&gt;requires more supervision&lt;/strong&gt;, not less.&lt;/p&gt;




&lt;h3&gt;
  
  
  😬 The Tech Gods Now Serve Investors, Not Innovation
&lt;/h3&gt;

&lt;p&gt;These so-called pioneers — Sundar with “AI-first” Google, Satya with Copilot baked into every Microsoft app, and Sam with his AGI daydreams — now &lt;strong&gt;serve&lt;/strong&gt; one god only: ROI. Not humanity. Not innovation. Not even good UX.&lt;/p&gt;

&lt;p&gt;They &lt;strong&gt;chase&lt;/strong&gt; profit over purpose. They &lt;strong&gt;pivot&lt;/strong&gt; features weekly to satisfy boardrooms. And while they &lt;strong&gt;claim&lt;/strong&gt; AI is the solution to everything, they still &lt;strong&gt;need&lt;/strong&gt; armies of humans to fact-check, correct, and rephrase AI outputs.&lt;/p&gt;

&lt;p&gt;So who’s doing the job again?&lt;/p&gt;




&lt;h3&gt;
  
  
  🪦 AI: The Emergency Exit for Failing Startups
&lt;/h3&gt;

&lt;p&gt;Open LinkedIn. Everyone &lt;strong&gt;sells&lt;/strong&gt; “AI-powered” tools. Doesn’t matter if it’s a slideshow maker, resume scanner, or glorified chatbot — they all &lt;strong&gt;claim&lt;/strong&gt; to use AI.&lt;/p&gt;

&lt;p&gt;Why? Because AI &lt;strong&gt;sells the dream&lt;/strong&gt; even when the product &lt;strong&gt;doesn’t work&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Most tools &lt;strong&gt;run&lt;/strong&gt; on wrappers around ChatGPT, slapped together with prompt engineering and desperation. If you’re a real company with real users, you still &lt;strong&gt;hire&lt;/strong&gt; designers, engineers, writers — because tools like these &lt;strong&gt;can’t&lt;/strong&gt; understand users, solve edge cases, or think critically.&lt;/p&gt;

&lt;p&gt;AI is not Step 1. It’s Plan Z.&lt;/p&gt;




&lt;h3&gt;
  
  
  💡 What Actually Remains?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;A few cool demos&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A trillion-dollar hype cycle&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Founders &lt;strong&gt;who pitch more than ship&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;And three tech giants &lt;strong&gt;who hope&lt;/strong&gt; their own tech doesn’t implode during a live demo&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Meanwhile, real people &lt;strong&gt;keep working&lt;/strong&gt;, &lt;strong&gt;keep building&lt;/strong&gt;, and &lt;strong&gt;keep creating value&lt;/strong&gt; AI still can’t replicate — because creativity and understanding &lt;strong&gt;don’t come from token prediction&lt;/strong&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  🚫 The Bottom Line
&lt;/h3&gt;

&lt;p&gt;AI &lt;strong&gt;doesn’t end jobs&lt;/strong&gt; — it &lt;strong&gt;forces us to question&lt;/strong&gt; how we define work, value, and trust. And the loudest believers — Sundar, Satya, and Sam — now &lt;strong&gt;look&lt;/strong&gt; less like prophets and more like &lt;strong&gt;salesmen who oversold a half-baked dream&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;They &lt;strong&gt;fear&lt;/strong&gt; what happens when the world wakes up and says: “Wait, this is it?”&lt;/p&gt;

&lt;p&gt;So don’t panic. Don’t submit. &lt;strong&gt;AI can assist — but it can’t replace you&lt;/strong&gt;.&lt;br&gt;&lt;br&gt;
Not today. Not tomorrow. Maybe not ever.&lt;/p&gt;

&lt;p&gt;And deep down, even Sundar, Satya, and Sam &lt;strong&gt;know it&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>ai</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Multithreading, Concurrency: A Deep Dive with JavaScript</title>
      <dc:creator>Rahul Vijayvergiya</dc:creator>
      <pubDate>Tue, 04 Mar 2025 12:06:07 +0000</pubDate>
      <link>https://dev.to/rahulvijayvergiya/multithreading-concurrency-a-deep-dive-with-javascript-1gkp</link>
      <guid>https://dev.to/rahulvijayvergiya/multithreading-concurrency-a-deep-dive-with-javascript-1gkp</guid>
      <description>&lt;p&gt;This post was initially published on &lt;a href="https://rahulvijayvergiya.hashnode.dev/" rel="noopener noreferrer"&gt;my blog&lt;/a&gt;. Check out the original source using the link below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://rahulvijayvergiya.hashnode.dev/multithreading-concurrency-a-deep-dive-with-javascript" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Frahulvijayvergiya.hashnode.dev%2Fapi%2Fog%2Fpost%3Fog%3DeyJ0aXRsZSI6Ik11bHRpdGhyZWFkaW5nJTJDJTIwQ29uY3VycmVuY3klM0ElMjBBJTIwRGVlcCUyMERpdmUlMjB3aXRoJTIwSmF2YVNjcmlwdCIsImF1dGhvciI6IlJhaHVsJTIwVmlqYXl2ZXJnaXlhIiwiZG9tYWluIjoicmFodWx2aWpheXZlcmdpeWEuaGFzaG5vZGUuZGV2IiwicGhvdG8iOiJodHRwczovL2Nkbi5oYXNobm9kZS5jb20vcmVzL2hhc2hub2RlL2ltYWdlL3VwbG9hZC92MTcyMTg4MzQ2NzIwMy9kand0ZzlDSDMuanBlZyIsInJlYWRUaW1lIjozfQ%3D%3D" height="400" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://rahulvijayvergiya.hashnode.dev/multithreading-concurrency-a-deep-dive-with-javascript" rel="noopener noreferrer" class="c-link"&gt;
          Multithreading, Concurrency: A Deep Dive with JavaScript
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          This article discusses how javascript handle Multithreading and Concurrency
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1611242173172%2FAOX1gE2jc.png" width="32" height="32"&gt;
        rahulvijayvergiya.hashnode.dev
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;Ever wondered how your computer handles multiple tasks at once? Whether it's running multiple apps, loading web pages, or processing data, it all comes down to &lt;strong&gt;multithreading&lt;/strong&gt; and &lt;strong&gt;concurrency&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;But what exactly do these terms mean, and how do they work? Let’s break it down in a way that’s easy to understand, with a special focus on how JavaScript—despite being single-threaded—handles concurrency like a pro.&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Multithreading&lt;/strong&gt; allows a CPU or program to execute multiple threads simultaneously. A &lt;strong&gt;thread&lt;/strong&gt; is the smallest unit of execution within a process, and multithreading helps in parallelising tasks, leading to better performance and resource efficiency.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Key Concepts in Multithreading&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Thread&lt;/strong&gt; – A lightweight unit of execution within a process.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Process&lt;/strong&gt; – A running program with its own memory space.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Thread Scheduling&lt;/strong&gt; – The operating system decides which thread runs when.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Context Switching&lt;/strong&gt; – Storing the state of one thread while switching to another.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Why Use Multithreading?&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;🚀 &lt;strong&gt;Performance Boost&lt;/strong&gt; – Multiple tasks run in parallel.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;🏗️ &lt;strong&gt;Efficient Resource Use&lt;/strong&gt; – Threads share memory, reducing overhead.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;🖥️ &lt;strong&gt;Responsive Applications&lt;/strong&gt; – Keeps UI applications smooth and lag-free.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;🔬 &lt;strong&gt;Parallel Computing&lt;/strong&gt; – Great for CPU-heavy tasks like AI, simulations, and gaming.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Challenges of Multithreading&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Race Conditions&lt;/strong&gt; – Multiple threads accessing shared data can cause unpredictable behavior.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Deadlocks&lt;/strong&gt; – Threads waiting indefinitely for each other to release resources.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Synchronisation Overhead&lt;/strong&gt; – Managing shared resources requires extra work (locks, semaphores, mutexes, etc.).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Concurrency is about managing multiple tasks at the same time, but not necessarily executing them in parallel. A system that supports concurrency can switch between tasks efficiently, even if it has only one thread.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Key Concepts in Concurrency&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Asynchronous Execution&lt;/strong&gt; – Tasks run without blocking each other.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Event Loop&lt;/strong&gt; – A single-threaded mechanism to handle multiple operations efficiently.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Non-Blocking I/O&lt;/strong&gt; – Prevents slow I/O operations from blocking execution.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Task Scheduling&lt;/strong&gt; – Determines the execution order of different tasks.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Why Concurrency Matters?&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;🔄 &lt;strong&gt;Faster Execution&lt;/strong&gt; – Keeps applications running smoothly by handling multiple tasks efficiently.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;🌍 &lt;strong&gt;Better Scalability&lt;/strong&gt; – Essential for handling many client requests in web applications.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;⚡ &lt;strong&gt;Non-Blocking Performance&lt;/strong&gt; – Ideal for real-time apps like chat, gaming, and video streaming.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Challenges of Concurrency&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Synchronisation Issues&lt;/strong&gt; – Ensuring correct execution order can be tricky.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Data Races&lt;/strong&gt; – Tasks modifying shared data at the same time can lead to conflicts.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Debugging Complexity&lt;/strong&gt; – Bugs related to concurrency are often hard to trace.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Multithreading and Concurrency in JavaScript
&lt;/h2&gt;

&lt;p&gt;JavaScript is &lt;strong&gt;single-threaded&lt;/strong&gt; by design, meaning it executes one task at a time. However, it achieves concurrency using an &lt;strong&gt;event-driven, non-blocking architecture&lt;/strong&gt; powered by the &lt;strong&gt;event loop&lt;/strong&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;How JavaScript Manages Concurrency&lt;/strong&gt;
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Event Loop&lt;/strong&gt; – Manages async operations efficiently.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Callbacks &amp;amp; Promises&lt;/strong&gt; – Allow non-blocking execution.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Async/Await&lt;/strong&gt; – A cleaner syntax for handling asynchronous code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Web Workers&lt;/strong&gt; – Enable multithreading in JavaScript.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Example: Concurrency in JavaScript&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&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;Start&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;Async Task Completed&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;2000&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;End&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;strong&gt;Output:&lt;/strong&gt;&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;Start&lt;/span&gt;
&lt;span class="nx"&gt;End&lt;/span&gt;
&lt;span class="nx"&gt;Async&lt;/span&gt; &lt;span class="nx"&gt;Task&lt;/span&gt; &lt;span class="nc"&gt;Completed &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;after&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="nx"&gt;seconds&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Even though &lt;code&gt;setTimeout&lt;/code&gt; is scheduled first, JavaScript doesn’t wait for it to complete—it moves on to the next task. The event loop ensures smooth execution without blocking.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Using Web Workers for Multithreading in JavaScript&lt;/strong&gt;
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// main.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;worker&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;Worker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;worker.js&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onmessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&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="s2"&gt;Message from Worker:&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="nx"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;postMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Hello Worker&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// worker.js&lt;/span&gt;
&lt;span class="nx"&gt;onmessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;postMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Worker received: &lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&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;Web Workers let JavaScript run scripts in background threads, preventing the main thread from getting blocked by heavy computations.&lt;/p&gt;

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

&lt;p&gt;While JavaScript doesn’t support traditional multithreading, it leverages &lt;strong&gt;event-driven concurrency&lt;/strong&gt; to handle multiple tasks efficiently. If you need actual parallel execution, &lt;strong&gt;Web Workers&lt;/strong&gt; can help!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>node</category>
      <category>programming</category>
    </item>
    <item>
      <title>Understanding LibUV and Its Thread Pool</title>
      <dc:creator>Rahul Vijayvergiya</dc:creator>
      <pubDate>Tue, 11 Feb 2025 18:27:12 +0000</pubDate>
      <link>https://dev.to/rahulvijayvergiya/understanding-libuv-and-its-thread-pool-69i</link>
      <guid>https://dev.to/rahulvijayvergiya/understanding-libuv-and-its-thread-pool-69i</guid>
      <description>&lt;p&gt;This post was initially published on &lt;a href="https://rahulvijayvergiya.hashnode.dev/" rel="noopener noreferrer"&gt;my blog&lt;/a&gt;. Check out the original source using the link below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://rahulvijayvergiya.hashnode.dev/understanding-libuv-and-its-thread-pool" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Frahulvijayvergiya.hashnode.dev%2Fapi%2Fog%2Fpost%3Fog%3DeyJ0aXRsZSI6IlVuZGVyc3RhbmRpbmclMjBMaWJVViUyMGFuZCUyMEl0cyUyMFRocmVhZCUyMFBvb2wiLCJhdXRob3IiOiJSYWh1bCUyMFZpamF5dmVyZ2l5YSIsImRvbWFpbiI6InJhaHVsdmlqYXl2ZXJnaXlhLmhhc2hub2RlLmRldiIsInBob3RvIjoiaHR0cHM6Ly9jZG4uaGFzaG5vZGUuY29tL3Jlcy9oYXNobm9kZS9pbWFnZS91cGxvYWQvdjE3MjE4ODM0NjcyMDMvZGp3dGc5Q0gzLmpwZWciLCJyZWFkVGltZSI6M30%3D" height="400" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://rahulvijayvergiya.hashnode.dev/understanding-libuv-and-its-thread-pool" rel="noopener noreferrer" class="c-link"&gt;
          Understanding LibUV and Its Thread Pool
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          In this post, we'll explore what LibUV does and dive into one of its essential features: the thread pool.

        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1611242173172%2FAOX1gE2jc.png" width="32" height="32"&gt;
        rahulvijayvergiya.hashnode.dev
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;Even if you've never heard of LibUV, , If you've worked with Node.js, you've already benefited from &lt;strong&gt;it.&lt;/strong&gt; LibUV is the engine that powers Node.js' asynchronous operations, making it possible to handle non-blocking I/O efficiently.&lt;/p&gt;

&lt;p&gt;In this post, we'll explore what LibUV does and dive into one of its essential features: &lt;strong&gt;the thread pool&lt;/strong&gt;.&lt;/p&gt;

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

&lt;p&gt;LibUV is a C library that handles asynchronous I/O operations. While it was originally created for Node.js, it is now used by other projects as well. Its main responsibilities include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Event Loop:&lt;/strong&gt; The heart of Node.js, managing asynchronous operations.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Asynchronous I/O:&lt;/strong&gt; Handling file system operations, DNS, networking, and more.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Thread Pool:&lt;/strong&gt; Running CPU-bound or blocking tasks on separate threads.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Since JavaScript is single-threaded, LibUV helps Node.js perform non-blocking tasks without freezing the main thread.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Role of the Thread Pool in LibUV
&lt;/h2&gt;

&lt;p&gt;While many Node.js operations are truly asynchronous (like networking), some are inherently &lt;strong&gt;blocking&lt;/strong&gt;—such as file system operations, cryptographic functions, and DNS lookups. These cannot be performed on the main thread without slowing down the entire application. This is where &lt;strong&gt;LibUV’s thread pool&lt;/strong&gt; comes into play. It provides a pool of worker threads to handle expensive operations without blocking the event loop.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Let's imagine you have a task that takes a while to complete, like reading a large file from your hard drive. If your application waited for that file to be read completely before doing anything else, it would freeze and become unresponsive. This is where the thread pool comes in. LibUV can offload these time-consuming tasks to a separate pool of threads.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  How It Works
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;When a blocking operation (e.g., reading a file) is triggered, LibUV offloads it to the thread pool.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A worker thread picks up the task and executes it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once completed, the result is passed back to the event loop, which then invokes the corresponding callback in JavaScript.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Example: File Reading with Thread Pool
&lt;/h3&gt;

&lt;p&gt;Here’s a simple Node.js example that uses the thread pool:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fs&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;Start reading file...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;example.txt&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="s1"&gt;utf-8&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="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&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;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Error reading file&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;err&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;}&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;File contents:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&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;Reading file initiated, non-blocking!&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;Even though &lt;code&gt;fs.readFile&lt;/code&gt; is a blocking operation under the hood, it gets offloaded to the thread pool, keeping the event loop free.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Points About the Thread Pool
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The default size of the thread pool is &lt;strong&gt;4 threads&lt;/strong&gt; (can be modified via &lt;code&gt;UV_THREADPOOL_SIZE&lt;/code&gt; environment variable).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Not all async operations use the thread pool. For example, network requests are handled by the operating system and do not require threads.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The thread pool is essential for performance, especially for CPU-intensive tasks.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  When to Be Cautious
&lt;/h2&gt;

&lt;p&gt;While the thread pool is powerful, there are a few things to watch out for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Large Thread Pool:&lt;/strong&gt; Increasing thread pool size can help, but too many threads can lead to excessive CPU usage and context switching.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;CPU-Intensive Workloads:&lt;/strong&gt; If a task is purely CPU-bound (e.g., heavy computations), consider using worker threads instead of the LibUV thread pool.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;LibUV plays a crucial role in making Node.js efficient, and its &lt;strong&gt;thread pool&lt;/strong&gt; is a key feature for handling blocking operations in an asynchronous way.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>node</category>
    </item>
    <item>
      <title>Under the Hood of Node.js: Exploring the V8 JavaScript Engine</title>
      <dc:creator>Rahul Vijayvergiya</dc:creator>
      <pubDate>Tue, 11 Feb 2025 18:11:03 +0000</pubDate>
      <link>https://dev.to/rahulvijayvergiya/under-the-hood-of-nodejs-exploring-the-v8-javascript-engine-4e6p</link>
      <guid>https://dev.to/rahulvijayvergiya/under-the-hood-of-nodejs-exploring-the-v8-javascript-engine-4e6p</guid>
      <description>&lt;p&gt;This post was initially published on &lt;a href="https://rahulvijayvergiya.hashnode.dev/" rel="noopener noreferrer"&gt;my blog&lt;/a&gt;. Check out the original source using the link below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://rahulvijayvergiya.hashnode.dev/under-the-hood-of-nodejs-exploring-the-v8-javascript-engine" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fhashnode.com%2Futility%2Fr%3Furl%3Dhttps%253A%252F%252Fcdn.hashnode.com%252Fres%252Fhashnode%252Fimage%252Fupload%252Fv1739297534496%252Fdf066116-deae-41d8-b446-0834784ac0fe.png%253Fw%253D1200%2526h%253D630%2526fit%253Dcrop%2526crop%253Dentropy%2526auto%253Dcompress%252Cformat%2526format%253Dwebp%2526fm%253Dpng" height="457" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://rahulvijayvergiya.hashnode.dev/under-the-hood-of-nodejs-exploring-the-v8-javascript-engine" rel="noopener noreferrer" class="c-link"&gt;
          Exploring the V8 JavaScript Engine
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          what exactly is V8, and how does it make Node.js so efficient? In this article we will explores the inner workings of V8.
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1611242173172%2FAOX1gE2jc.png" width="32" height="32"&gt;
        rahulvijayvergiya.hashnode.dev
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;V8 engine is at the heart of executing JavaScript. It’s is an open-source, high-performance JavaScript and WebAssembly engine written in C++. It is used in Google Chrome and Node.js, enabling fast JavaScript execution both in the browser and on the server side.&lt;/p&gt;

&lt;p&gt;But what exactly is V8, and how does it make Node.js so efficient? In this article we will explores the inner workings of V8.&lt;/p&gt;

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

&lt;p&gt;v8’s primary function is to take JavaScript code and execute it. However, V8 doesn't just interpret the code line by line, It includes features like Just-In-Time (JIT) compilation, garbage collection, and optimisation techniques to improve performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  From JavaScript to Machine Code
&lt;/h2&gt;

&lt;p&gt;Traditionally, JavaScript was interpreted, meaning each line of code was executed one at a time. This process can be slow. V8, on the other hand, uses a Just-In-Time (JIT) compiler.&lt;/p&gt;

&lt;h2&gt;
  
  
  The V8 Execution Process
&lt;/h2&gt;

&lt;p&gt;Here's a simplified breakdown of the process:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Parsing:&lt;/strong&gt; V8 first parses the JavaScript code, creating an Abstract Syntax Tree (AST). This tree represents the structure of the code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Interpreter&lt;/strong&gt;: V8's interpreter, takes the AST and converts it into bytecode. Bytecode is an intermediate representation that is easier to execute than raw JavaScript but not as fast as machine code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Compilation (Crankshaft)&lt;/strong&gt;: Initially, V8 uses a baseline compiler (called Crankshaft in older versions, but now Turbofan is the primary optimising compiler). This compiler quickly generates machine code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Optimization&lt;/strong&gt; : V8 monitors the running code. If it identifies frequently executed "hot paths" (parts of the code that are run often), it uses a more sophisticated optimising compiler (Turbofan) to generate highly optimised machine code. This optimisation happens in the background while the code is running.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Deoptimisation&lt;/strong&gt;: Sometimes, V8 makes assumptions during optimisation. If these assumptions turn out to be incorrect (e.g., a variable's type changes), V8 can deoptimise the code and revert to the baseline compiler. This ensures correctness.&lt;/p&gt;

&lt;h2&gt;
  
  
  V8 and Node.js Performance:
&lt;/h2&gt;

&lt;p&gt;V8's efficiency directly translates to Node.js's performance. The JIT compilation and other optimisations allow Node.js applications to run quickly and handle a large number of concurrent requests. This makes Node.js suitable for building high-performance web servers and other applications.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further Reading:
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.freecodecamp.org/news/javascript-under-the-hood-v8/" rel="noopener noreferrer"&gt;https://www.freecodecamp.org/news/javascript-under-the-hood-v8/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://hyperskill.org/learn/step/41307" rel="noopener noreferrer"&gt;https://hyperskill.org/learn/step/41307&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.geeksforgeeks.org/explain-the-role-of-v8-engine-in-nodejs/" rel="noopener noreferrer"&gt;https://www.geeksforgeeks.org/explain-the-role-of-v8-engine-in-nodejs/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;By using JIT compilation, garbage collection, and optimisations like inline caching, V8 ensures JavaScript runs at high speed both in browsers and in Node.js.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>node</category>
      <category>react</category>
    </item>
    <item>
      <title>SAML vs. OAuth vs. OpenID Connect</title>
      <dc:creator>Rahul Vijayvergiya</dc:creator>
      <pubDate>Wed, 16 Oct 2024 06:47:22 +0000</pubDate>
      <link>https://dev.to/rahulvijayvergiya/saml-vs-oauth-vs-openid-connect-29m4</link>
      <guid>https://dev.to/rahulvijayvergiya/saml-vs-oauth-vs-openid-connect-29m4</guid>
      <description>&lt;p&gt;This post was initially published on my &lt;a href="https://rahulvijayvergiya.hashnode.dev/" rel="noopener noreferrer"&gt;blog&lt;/a&gt;. Check out the original source using the link below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://rahulvijayvergiya.hashnode.dev/saml-vs-oauth-vs-openid-connect" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Frahulvijayvergiya.hashnode.dev%2Fapi%2Fog%2Fpost%3Fog%3DeyJ0aXRsZSI6IlNBTUwlMjB2cy4lMjBPQXV0aCUyMHZzLiUyME9wZW5JRCUyMENvbm5lY3QiLCJhdXRob3IiOiJSYWh1bCUyMFZpamF5dmVyZ2l5YSIsImRvbWFpbiI6InJhaHVsdmlqYXl2ZXJnaXlhLmhhc2hub2RlLmRldiIsInBob3RvIjoiaHR0cHM6Ly9jZG4uaGFzaG5vZGUuY29tL3Jlcy9oYXNobm9kZS9pbWFnZS91cGxvYWQvdjE3MjE4ODM0NjcyMDMvZGp3dGc5Q0gzLmpwZWciLCJyZWFkVGltZSI6NH0%3D" height="400" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://rahulvijayvergiya.hashnode.dev/saml-vs-oauth-vs-openid-connect" rel="noopener noreferrer" class="c-link"&gt;
          SAML vs. OAuth vs. OpenID Connect
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          This article will explore SAML, OAuth and OpenID Connect, their use cases, and how they interact with one another
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1611242173172%2FAOX1gE2jc.png" width="32" height="32"&gt;
        rahulvijayvergiya.hashnode.dev
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;Authentication and Authorisation are crucial for ensuring the security of applications and data. Three widely-used protocols for managing these processes are OAuth, SAML, and OpenID.&lt;/p&gt;

&lt;p&gt;When you use apps like Facebook, Google, or LinkedIn to log into other websites, you’re encountering some important protocols: &lt;strong&gt;OAuth&lt;/strong&gt;, &lt;strong&gt;SAML&lt;/strong&gt;, and &lt;strong&gt;OpenID Connect&lt;/strong&gt;. These protocols help keep your information secure while allowing you to access different services.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. SAML (Security Assertion Markup Language)
&lt;/h2&gt;

&lt;p&gt;SAML is an open standard for exchanging authentication and authorization data between parties, particularly between an identity provider (IdP) and a service provider (SP). It is primarily used for single sign-on (SSO) scenarios, enabling users to authenticate once and gain access to multiple applications.&lt;/p&gt;

&lt;h3&gt;
  
  
  How SAML Works
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Roles/Actors:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;User:&lt;/strong&gt; The individual accessing the applications.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Identity Provider (IdP):&lt;/strong&gt; The service that authenticates the user and provides the identity assertions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Service Provider (SP):&lt;/strong&gt; The application or service the user is trying to access.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Request Flow Diagram:&lt;/strong&gt;&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%2F0fknvujcj16ghs6xbdqg.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%2F0fknvujcj16ghs6xbdqg.png" alt="SAML Flow Diagram" width="437" height="469"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Features of SAML
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Single Sign-On (SSO):&lt;/strong&gt; Users authenticate once for multiple services.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;XML-Based:&lt;/strong&gt; Uses XML for messages, making it robust.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Secure Assertions:&lt;/strong&gt; Transmits user identity and attributes securely. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  2. OAuth (Open Authorisation)
&lt;/h2&gt;

&lt;p&gt;OAuth is an open standard for access delegation commonly used for token-based authentication and authorisation. It allows third-party applications to access user data without exposing their credentials. OAuth is primarily used for authorisation, enabling users to grant limited access to their resources hosted on one site to another site.&lt;/p&gt;

&lt;h3&gt;
  
  
  How OAuth Works
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Roles/Actors:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Resource Owner:&lt;/strong&gt; The user who owns the data and grants access to it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Resource Server:&lt;/strong&gt; The server hosting the user's data (e.g., Google, Facebook).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Client:&lt;/strong&gt; The third-party application requesting access to the user's data.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Authorization Server:&lt;/strong&gt; The server responsible for issuing access tokens to the client after authenticating the resource owner.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Request Flow Diagram:&lt;/strong&gt;&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%2Fupx4lzb9fmm6mto08mz2.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%2Fupx4lzb9fmm6mto08mz2.png" alt="OAuth Flow Diagram" width="647" height="518"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Features of OAuth
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Delegated Access:&lt;/strong&gt; Users can grant access without sharing credentials.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Access Tokens:&lt;/strong&gt; Short-lived tokens that limit access duration.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Scopes:&lt;/strong&gt; Define the extent of access granted to the client.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  3. OpenID Connect
&lt;/h2&gt;

&lt;p&gt;OpenID Connect is an authentication layer built on top of OAuth 2.0. It adds identity verification and provides a way for clients to verify the identity of the user based on the authentication performed by an Authorisation Server.&lt;/p&gt;

&lt;h3&gt;
  
  
  How OpenID Connect Works
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;Roles/Actors:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;End User:&lt;/strong&gt; The user who wants to authenticate.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Client:&lt;/strong&gt; The application requesting user authentication.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Authorisation Server:&lt;/strong&gt; The server that authenticates the user and issues tokens.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Request Flow Diagram:&lt;/strong&gt;&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%2Fg2gk62bk4dl91bz7bvxq.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%2Fg2gk62bk4dl91bz7bvxq.png" alt="OpenID Flow Diagram" width="631" height="518"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Key Features of OpenID Connect
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;ID Tokens:&lt;/strong&gt; JWTs that provide user information and claims.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Seamless Integration with OAuth:&lt;/strong&gt; Combines authentication and authorisation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;User Info Endpoint:&lt;/strong&gt; Allows fetching additional user information.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;OpenID Connect&lt;/strong&gt; and &lt;strong&gt;OAuth&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Seems similar but they both serve different but complementary purposes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;OAuth&lt;/strong&gt;: Used for &lt;strong&gt;authorisation&lt;/strong&gt;. It allows apps to access resources (like your profile or photos) from another service (e.g., Facebook or Google) without needing your password. Use OAuth when you want to &lt;strong&gt;grant limited access&lt;/strong&gt; to your data (e.g., a third-party app accessing your calendar).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;OpenID Connect (OIDC)&lt;/strong&gt;: Built on top of OAuth, it's used for &lt;strong&gt;authentication&lt;/strong&gt;. It verifies your &lt;strong&gt;identity&lt;/strong&gt; and allows apps to know who you are. Use OpenID Connect when you need to &lt;strong&gt;log in&lt;/strong&gt; to a website or app using a service like Google, proving who you are.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  When to Use:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Use OAuth&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When you want an app to access data or resources on your behalf (e.g., an app posting on Twitter for you).&lt;/li&gt;
&lt;li&gt;"Can this app access my data?" (Authorisation)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Use OpenID Connect&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When you need to verify a user's identity (e.g., signing into a website using your Google account).&lt;/li&gt;
&lt;li&gt;"Who is this user?" (Authentication)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  Quick Comparison of OAuth, SAML, and OpenID Connect
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Feature&lt;/th&gt;
&lt;th&gt;OAuth&lt;/th&gt;
&lt;th&gt;SAML&lt;/th&gt;
&lt;th&gt;OpenID Connect&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Purpose&lt;/td&gt;
&lt;td&gt;Authorization&lt;/td&gt;
&lt;td&gt;Authentication&lt;/td&gt;
&lt;td&gt;Authentication &amp;amp; Authorization&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Format&lt;/td&gt;
&lt;td&gt;JSON, Token&lt;/td&gt;
&lt;td&gt;XML&lt;/td&gt;
&lt;td&gt;JSON&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Use Cases&lt;/td&gt;
&lt;td&gt;API access, Mobile apps&lt;/td&gt;
&lt;td&gt;Web applications, SSO&lt;/td&gt;
&lt;td&gt;Web and mobile applications&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Complexity&lt;/td&gt;
&lt;td&gt;Moderate&lt;/td&gt;
&lt;td&gt;High&lt;/td&gt;
&lt;td&gt;Moderate&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;User Experience&lt;/td&gt;
&lt;td&gt;Redirects to IdP&lt;/td&gt;
&lt;td&gt;Redirects to IdP&lt;/td&gt;
&lt;td&gt;Redirects to Authorisation Server&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

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

&lt;p&gt;Understanding OAuth, SAML, and OpenID Connect is essential for building secure applications that handle user authentication and authorisation effectively. Each protocol has its purpose, ensuring you can access what you need while keeping your data safe.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://oauth.net/2/" rel="noopener noreferrer"&gt;OAuth 2.0 Explained Simply&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.onelogin.com/learn/saml" rel="noopener noreferrer"&gt;What is SAML?&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://openid.net/connect/" rel="noopener noreferrer"&gt;OpenID Connect Overview&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://auth0.com/blog/saml-vs-oauth-vs-openid-connect/" rel="noopener noreferrer"&gt;Understanding the Differences: OAuth, SAML, and OpenID Connect&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>node</category>
      <category>react</category>
    </item>
    <item>
      <title>JavaScript Prototypes and Inheritance</title>
      <dc:creator>Rahul Vijayvergiya</dc:creator>
      <pubDate>Wed, 28 Aug 2024 09:35:58 +0000</pubDate>
      <link>https://dev.to/rahulvijayvergiya/javascript-prototypes-and-inheritance-4okb</link>
      <guid>https://dev.to/rahulvijayvergiya/javascript-prototypes-and-inheritance-4okb</guid>
      <description>&lt;p&gt;This post was initially published on my &lt;a href="https://rahulvijayvergiya.hashnode.dev" rel="noopener noreferrer"&gt;blog&lt;/a&gt;. Check out the original source using the link below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://rahulvijayvergiya.hashnode.dev/javascript-prototypes-and-inheritance" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Frahulvijayvergiya.hashnode.dev%2Fapi%2Fog%2Fpost%3Fog%3DeyJ0aXRsZSI6IkphdmFTY3JpcHQlMjBQcm90b3R5cGVzJTIwYW5kJTIwSW5oZXJpdGFuY2UiLCJhdXRob3IiOiJSYWh1bCUyMFZpamF5dmVyZ2l5YSIsImRvbWFpbiI6InJhaHVsdmlqYXl2ZXJnaXlhLmhhc2hub2RlLmRldiIsInBob3RvIjoiaHR0cHM6Ly9jZG4uaGFzaG5vZGUuY29tL3Jlcy9oYXNobm9kZS9pbWFnZS91cGxvYWQvdjE3MjE4ODM0NjcyMDMvZGp3dGc5Q0gzLmpwZWciLCJyZWFkVGltZSI6Nn0%3D" height="400" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://rahulvijayvergiya.hashnode.dev/javascript-prototypes-and-inheritance" rel="noopener noreferrer" class="c-link"&gt;
          JavaScript Prototypes and Inheritance
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          In this blog, we ha break down the concepts of prototypes and inheritance in JavaScript in a friendly and easy-to-understand manner, with examples to help s
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1611242173172%2FAOX1gE2jc.png" width="32" height="32"&gt;
        rahulvijayvergiya.hashnode.dev
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;JavaScript is unique language, it doesn't use classical inheritance like languages such as Java or C++. Instead, it relies on prototypes. When i initially started learning javascript Prototype was always a confusing topic for me.&lt;/p&gt;

&lt;p&gt;Even though with the advancement of es6, writing classes in javascript is much easier but the fact is that prototypes and inheritance are the foundation of JavaScript's object model, understanding them is essential for writing more effective and interoperable code,&lt;/p&gt;

&lt;p&gt;In this blog, I'll break down the concepts of prototypes and inheritance in JavaScript in a friendly and easy-to-understand manner.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are Prototypes in javascript?
&lt;/h2&gt;

&lt;p&gt;In JavaScript, every object has a prototype. A prototype is another object that the original object inherits properties and methods from. We can think of it as a blueprint or a template that provides default values for the object.&lt;/p&gt;

&lt;p&gt;Let’s take an example to make this clearer:&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;// Creating a simple object&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;person&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&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="s2"&gt;Hello!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Creating another object that inherits from 'person'&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;john&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;person&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Calling the greet method on 'john'&lt;/span&gt;
&lt;span class="nx"&gt;john&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Output: "Hello!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, john doesn't have a greet method on its own, but it can still call greet because it inherits from the person object. The greet method is part of the person prototype, and john has access to it.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Prototypal Inheritance Works
&lt;/h2&gt;

&lt;p&gt;When we try to access a property or method on an object, JavaScript first checks if that property or method exists on the object itself. If it doesn't, JavaScript then looks at the object's prototype to see if the property or method exists there. This process continues up the prototype chain until the property or method is found or until the chain ends.&lt;/p&gt;

&lt;p&gt;Here’s another example to explain this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;animal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Mammal&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;sound&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&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="s2"&gt;Generic animal sound&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="p"&gt;};&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dog&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;animal&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bark&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&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="s2"&gt;Woof!&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="nx"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sound&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Output: "Generic animal sound"&lt;/span&gt;
&lt;span class="nx"&gt;dog&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bark&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;   &lt;span class="c1"&gt;// Output: "Woof!"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the dog object inherits from animal via the Object.create method. Even though dog does not directly have the sound method, it can access it through its prototype (animal). This is the core idea behind prototypes and inheritance in JavaScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Javascript Constructor Functions and Prototypes
&lt;/h2&gt;

&lt;p&gt;In JavaScript, you can use constructor functions to create objects that share the same prototype. Constructor functions are regular functions that are intended to be used with the &lt;code&gt;new&lt;/code&gt; keyword. When you use the &lt;code&gt;new&lt;/code&gt; keyword, JavaScript automatically sets the prototype of the new object to the &lt;code&gt;prototype&lt;/code&gt; property of the constructor function.&lt;/p&gt;

&lt;p&gt;Let’s see how this 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="c1"&gt;// Constructor function&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Person&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;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&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="c1"&gt;// Adding a method to the prototype&lt;/span&gt;
&lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;greet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&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="s2"&gt;`Hello, my name is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&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="s2"&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;// Creating a new object using the constructor&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;alice&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;Person&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="c1"&gt;// Accessing properties and methods&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;alice&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="c1"&gt;// Output: "Alice"&lt;/span&gt;
&lt;span class="nx"&gt;alice&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Output: "Hello, my name is Alice"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, Person is a constructor function. When we create a new Person object with new Person("Alice"), JavaScript sets the '&lt;strong&gt;__proto__&lt;/strong&gt;' of the newly created object to &lt;strong&gt;Person.prototype&lt;/strong&gt;. This means alice can access the greet method even though it’s not defined directly on alice&lt;/p&gt;

&lt;h2&gt;
  
  
  Inheritance with Constructor Functions
&lt;/h2&gt;

&lt;p&gt;You can also create more complex inheritance structures using constructor functions. Suppose you want to create a Student constructor function that inherits from Person.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt;&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;// Person constructor function&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Person&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;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&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="c1"&gt;// Adding a method to the Person prototype&lt;/span&gt;
&lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;greet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&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="s2"&gt;`Hello, my name is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&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="s2"&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;// Student constructor function&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Student&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;course&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Call the Person constructor function with 'this'&lt;/span&gt;
  &lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&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="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;course&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;course&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Setting up inheritance by linking prototypes&lt;/span&gt;
&lt;span class="nx"&gt;Student&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Person&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;Student&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;constructor&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Student&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Adding a method to the Student prototype&lt;/span&gt;
&lt;span class="nx"&gt;Student&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;study&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&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="s2"&gt; is studying &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;course&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="c1"&gt;// Creating a new Student object&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bob&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;Student&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Bob&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;Mathematics&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Accessing properties and methods&lt;/span&gt;
&lt;span class="nx"&gt;bob&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Output: "Hello, my name is Bob"&lt;/span&gt;
&lt;span class="nx"&gt;bob&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;study&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Output: "Bob is studying Mathematics"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the Student constructor function inherits from the Person constructor function. We use &lt;a href="http://Person.call" rel="noopener noreferrer"&gt;Person.call&lt;/a&gt;(this, name) to call the Person constructor within the Student constructor, ensuring that the name property is set correctly. Then, we set up the inheritance by linking Student.prototype to Person.prototype using Object.create.&lt;/p&gt;

&lt;p&gt;Finally, we added a study method to the Student.prototype, and the bob object can now access both greet and study methods.&lt;/p&gt;

&lt;h3&gt;
  
  
  Object.create vs. Constructor Functions
&lt;/h3&gt;

&lt;p&gt;Both Object.create and constructor functions can be used to create objects that share behaviour, but they work differently.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Object.create:&lt;/strong&gt; It creates a new object with the specified prototype object and properties. It's straightforward and gives more control over the prototype chain.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Constructor Functions:&lt;/strong&gt; They initialize the object and set up the prototype chain automatically using the &lt;code&gt;new&lt;/code&gt; keyword. Constructor functions are often used for creating multiple similar objects.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Understanding __proto__, prototype
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;__proto__: The Internal Prototype Link&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;proto&lt;/strong&gt; is an internal property that every JavaScript object has. It points to the object's prototype (from which it inherits properties and methods).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When you try to access a property on an object, JavaScript first looks for the property on the object itself. If it doesn’t find it, it moves up the prototype chain by following the &lt;strong&gt;proto&lt;/strong&gt; link until it either finds the property or reaches the end of the chain (null).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;prototype: The Property of Constructor Functions&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Only functions (specifically, constructor functions) have prototype property. It's used to define methods and properties that should be shared by all instances created by the constructor function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When you create an object using the &lt;code&gt;new&lt;/code&gt; keyword with a constructor function, the object's __proto is automatically set to the constructor's prototype.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Object.prototype: The Default Top-Level Object
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;All JavaScript objects inherit from Object.prototype, which is the root of the prototype chain. This object contains basic methods like, which toString, valueOf, and hasOwnProperty etc which are defined in Object.prototype.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  ES6 Classes and Prototypes
&lt;/h2&gt;

&lt;p&gt;With the introduction of ES6, JavaScript now has a &lt;code&gt;class&lt;/code&gt; syntax, which provides a more familiar way to define constructor functions and inheritance. However, under the hood, the class syntax still uses prototypes.&lt;/p&gt;

&lt;p&gt;Here’s the previous example rewritten using ES6 classes:&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;// Person class&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&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;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&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="nf"&gt;greet&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="s2"&gt;`Hello, my name is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Student class that inherits from Person&lt;/span&gt;
&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&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;course&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&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="c1"&gt;// Call the parent class constructor&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;course&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;course&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;study&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="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&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="s2"&gt; is studying &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;course&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="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Creating a new Student object&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;charlie&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;Student&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Charlie&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;Physics&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Accessing properties and methods&lt;/span&gt;
&lt;span class="nx"&gt;charlie&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Output: "Hello, my name is Charlie"&lt;/span&gt;
&lt;span class="nx"&gt;charlie&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;study&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;  &lt;span class="c1"&gt;// Output: "Charlie is studying Physics"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;JavaScript's prototypal inheritance might seem confusing , but once you understand the basic concepts, it becomes a important tool for creating reusable and extensible code, enabling code reuse and better code organisation.&lt;/p&gt;

&lt;h2&gt;
  
  
  Further readings:
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Inheritance_and_the_prototype_chain&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://javascript.info/prototype-inheritance" rel="noopener noreferrer"&gt;https://javascript.info/prototype-inheritance&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.digitalocean.com/community/tutorials/understanding-prototypes-and-inheritance-in-javascript" rel="noopener noreferrer"&gt;https://www.digitalocean.com/community/tutorials/understanding-prototypes-and-inheritance-in-javascript&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://portswigger.net/web-security/prototype-pollution/javascript-prototypes-and-inheritance" rel="noopener noreferrer"&gt;https://portswigger.net/web-security/prototype-pollution/javascript-prototypes-and-inheritance&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
      <category>node</category>
    </item>
    <item>
      <title>JavaScript Memory Management: The Secret to a Clean and Fast App</title>
      <dc:creator>Rahul Vijayvergiya</dc:creator>
      <pubDate>Tue, 20 Aug 2024 09:08:34 +0000</pubDate>
      <link>https://dev.to/rahulvijayvergiya/javascript-memory-management-the-secret-to-a-clean-and-fast-app-1mh8</link>
      <guid>https://dev.to/rahulvijayvergiya/javascript-memory-management-the-secret-to-a-clean-and-fast-app-1mh8</guid>
      <description>&lt;p&gt;This post was initially published on my &lt;a href="https://rahulvijayvergiya.hashnode.dev" rel="noopener noreferrer"&gt;blog&lt;/a&gt;. Check out the original source using the link below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://rahulvijayvergiya.hashnode.dev/javascript-memory-management-the-secret-to-a-clean-and-fast-app" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Frahulvijayvergiya.hashnode.dev%2Fapi%2Fog%2Fpost%3Fog%3DeyJ0aXRsZSI6IkphdmFTY3JpcHQlMjBNZW1vcnklMjBNYW5hZ2VtZW50JTNBJTIwVGhlJTIwU2VjcmV0JTIwdG8lMjBhJTIwQ2xlYW4lMjBhbmQlMjBGYXN0JTIwQXBwIiwiYXV0aG9yIjoiUmFodWwlMjBWaWpheXZlcmdpeWEiLCJkb21haW4iOiJyYWh1bHZpamF5dmVyZ2l5YS5oYXNobm9kZS5kZXYiLCJwaG90byI6Imh0dHBzOi8vY2RuLmhhc2hub2RlLmNvbS9yZXMvaGFzaG5vZGUvaW1hZ2UvdXBsb2FkL3YxNzIxODgzNDY3MjAzL2Rqd3RnOUNIMy5qcGVnIiwicmVhZFRpbWUiOjZ9" height="400" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://rahulvijayvergiya.hashnode.dev/javascript-memory-management-the-secret-to-a-clean-and-fast-app" rel="noopener noreferrer" class="c-link"&gt;
          JavaScript Memory Management: The Secret to a Clean and Fast App
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          Understanding how JavaScript manages memory, including the role of garbage collection and memory leaks.
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1611242173172%2FAOX1gE2jc.png" width="32" height="32"&gt;
        rahulvijayvergiya.hashnode.dev
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;





&lt;p&gt;Ever wonder how JavaScript keeps our web apps running smoothly without turning user's computer into a digital hoarder’s house. All thanks to memory management and garbage collection. Think of it as a digital housekeeper that clean up behind the scenes. In this blog, we’ll dive into how JavaScript handles memory, why garbage collection matters, and how can we avoid the memory leaks which can make our app sluggish.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;Memory Allocation: Where Does All the Stuff Go?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;When we write code in JavaScript, memory allocation happens automatically. Imagine we’re moving into a new house. We’ve got boxes labeled "Clothes," "Kitchen Stuff," and "Random Things that Should be Probably Throw Away." When we unpack, we need to find places for everything. In JavaScript, memory allocation is a bit like unpacking those boxes. There are three main scenarios where memory gets allocated:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Variables:&lt;/strong&gt; When we declare a variable, JavaScript allocates memory to store its value. They are like socks and need a drawer to live in.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Objects and arrays :&lt;/strong&gt; Creating objects and arrays also requires memory allocation for storing data. They are like our kitchen appliances which take up more space and need proper storage.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Functions:&lt;/strong&gt; Memory is allocated when we define and execute functions. They are recipes, we pull them out when we need them, and then put them away once no longer needed.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Cookie Monster&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Memory drawer for the string "Cookie Monster"&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;snacks&lt;/span&gt; &lt;span class="o"&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;cookies&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;more cookies&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;even more cookies&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// A pantry for the array of snacks&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;eatSnack&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="s2"&gt;Om nom nom&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// A recipe for snacking&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;We don’t have to manually manage this memory—JavaScript does it for us. Just like how we wouldn’t leave our toaster in the living room, JavaScript makes sure everything’s in its place so our code can run smoothly.&lt;/p&gt;
&lt;h3&gt;
  
  
  2. Javascript &lt;strong&gt;Memory Life Cycle: Use It and Lose It&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;The life cycle of memory in JavaScript is like borrowing stuff from a friend. First, we take it (allocate memory), then use it (run our code), and finally, (should) give it back (release memory). If we forget to give things back, well, that’s when the trouble starts :)&lt;/p&gt;

&lt;p&gt;Memory in JavaScript goes through a simple life cycle:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Allocation:&lt;/strong&gt; When we create a variable, object, or function, memory is allocated.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Usage:&lt;/strong&gt; This allocated memory is used to store values and execute code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Release:&lt;/strong&gt; When no longer needed, it should be released to make room for new data.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;JavaScript tries to be a good friend by automatically cleaning up after we are done through garbage collection.&lt;/p&gt;
&lt;h3&gt;
  
  
  3. &lt;strong&gt;What is Garbage Collection?&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Garbage collection is the process of identifying and freeing up memory that is no longer in use. JavaScript’s garbage collector automatically removes data that our program no longer needs to prevent memory leaks.&lt;/p&gt;

&lt;p&gt;In a way its Javascript's Marie Kondo ;) that cleanup and organise things we don’t need anymore. It finds all the things we’ve left lying around that don’t "spark fun" (a.k.a. aren’t being used) and gets rid of them to free up space.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mark-and-Sweep Algorithm&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The garbage collector uses something called the &lt;strong&gt;Mark-and-Sweep&lt;/strong&gt; algorithm.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Marking:&lt;/strong&gt; The garbage collector starts by marking all the objects that are still reachable from the root (e.g., the global object or variables still in use). Just like the cleaner who first goes through entire house, marking everything that’s still useful ( favourite mug, that shirt we wear all the time).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Sweeping:&lt;/strong&gt; It then identifies objects that are not marked, meaning they’re no longer reachable, and removes them from memory. Just like a cleaner sweeps up everything we don’t need and gets rid of it.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;createSnack&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;snack&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cookie&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;flavor&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;chocolate chip&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="nx"&gt;snack&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;snack1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createSnack&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;snack2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createSnack&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// When we finish the first snack, it's time to get rid of the crumbs (memory).&lt;/span&gt;
&lt;span class="nx"&gt;snack1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&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, once when &lt;code&gt;snack1&lt;/code&gt; is set to &lt;code&gt;null&lt;/code&gt;, JavaScript’s knows we’re done with it and sweeps it away.&lt;/p&gt;
&lt;h3&gt;
  
  
  4. &lt;strong&gt;Memory Leaks: When Our House Starts to Overflow&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;If we keep holding onto stuff we don’t need, our house (memory) gets cluttered. The same way despite automatic garbage collection, memory leaks can still happen when references to unused objects persist. Here are some common scenarios that lead to memory leaks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Global Variables: The Socks Under the Bed&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Global variables stay in memory for the entire life of the application. They are like those socks that keep disappearing under the bed. They stay forever and take up space we didn’t even know.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;messyFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;forgottenSocks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;These are now global&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Whoops, no 'let', 'const', or 'var'—now it's everywhere!&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Unintended Closures: The Forgotten Sandwich&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With Closures, an inner function retains access to variables from an outer function, even after the outer function has finished execution. It's like that sandwich we accidentally left in our backpack.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;outerFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;bigSecret&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;This is a huge secret no one needs to know anymore&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;innerFunction&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bigSecret&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Why are we still holding onto this?&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;inner&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;outerFunction&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// The big secret is still around, cluttering up memory.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Event Listeners: The Party Guests Who Never Leave&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If we add event listeners but don’t remove them when they’re no longer needed, they can cause memory leaks because they keep references to elements that may have been removed from the DOM. Think of it like guests who refuse to go home after the party. We need to tell them it’s time to leave, or they’ll keep eating our snacks :D&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;let&lt;/span&gt; &lt;span class="nx"&gt;button&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;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;partyButton&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&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="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="s2"&gt;Party time!&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="c1"&gt;// If the button disappears, tell our guests to go home.&lt;/span&gt;
&lt;span class="nx"&gt;button&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;partyTime&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Detached DOM Elements: The Floating Ball in pool after party&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;DOM elements that are removed from the document but still referenced in our JavaScript code will not be garbage collected, leading to memory leaks.&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;let&lt;/span&gt; &lt;span class="nx"&gt;ball&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;getElementById&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;myChair&lt;/span&gt;&lt;span class="dl"&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="nx"&gt;body&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;removeChild&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ball&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// The ball is still in memory, just floating around!&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  5. &lt;strong&gt;Best Practices to Keep The Code Clean&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Always declare variables&lt;/strong&gt; (use &lt;code&gt;let&lt;/code&gt;, &lt;code&gt;const&lt;/code&gt;, or &lt;code&gt;var&lt;/code&gt;) to avoid creating global variables.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Clean up event listeners&lt;/strong&gt; when they are no longer needed, especially if the elements they were attached to are removed, so our guests don’t overstay.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Watch out for closures&lt;/strong&gt; that hold onto unnecessary secrets.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Use weak references&lt;/strong&gt; (&lt;code&gt;WeakMap&lt;/code&gt;, &lt;code&gt;WeakSet&lt;/code&gt;) when we can—they won’t stop the garbage collector from doing its thing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Manually nullify references&lt;/strong&gt; to things (object, variable) we don’t need anymore, so our house doesn’t overflow.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;Conclusion: Keep It Clean, Keep It Fun&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;JavaScript’s memory management is like having a good friend who helps us stay organised. But even the best of friends need some guidance. By being mindful and following some simple practices, we can keep our app running smoothly and prevent it from turning into a hoarder’s house. So lets declutter code and enjoy a clean, efficient app!&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;strong&gt;References&lt;/strong&gt;
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_management" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Memory_management&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://medium.com/sessionstack-blog/how-javascript-works-memory-management-how-to-handle-4-common-memory-leaks-3f28b94cfbec" rel="noopener noreferrer"&gt;https://medium.com/sessionstack-blog/how-javascript-works-memory-management-how-to-handle-4-common-memory-leaks-3f28b94cfbec&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.turing.com/kb/handling-memory-management-in-javascript" rel="noopener noreferrer"&gt;https://www.turing.com/kb/handling-memory-management-in-javascript&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.calibraint.com/blog/garbage-collection-in-javascript" rel="noopener noreferrer"&gt;https://www.calibraint.com/blog/garbage-collection-in-javascript&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.geeksforgeeks.org/mark-and-sweep-garbage-collection-algorithm/" rel="noopener noreferrer"&gt;https://www.geeksforgeeks.org/mark-and-sweep-garbage-collection-algorithm/&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://www.tutorialspoint.com/explain-in-detail-about-mark-and-sweep-algorithm-in-javascript" rel="noopener noreferrer"&gt;https://www.tutorialspoint.com/explain-in-detail-about-mark-and-sweep-algorithm-in-javascript&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;




&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://rahulvijayvergiya.hashnode.dev/javascript-memory-management-the-secret-to-a-clean-and-fast-app" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Frahulvijayvergiya.hashnode.dev%2Fapi%2Fog%2Fpost%3Fog%3DeyJ0aXRsZSI6IkphdmFTY3JpcHQlMjBNZW1vcnklMjBNYW5hZ2VtZW50JTNBJTIwVGhlJTIwU2VjcmV0JTIwdG8lMjBhJTIwQ2xlYW4lMjBhbmQlMjBGYXN0JTIwQXBwIiwiYXV0aG9yIjoiUmFodWwlMjBWaWpheXZlcmdpeWEiLCJkb21haW4iOiJyYWh1bHZpamF5dmVyZ2l5YS5oYXNobm9kZS5kZXYiLCJwaG90byI6Imh0dHBzOi8vY2RuLmhhc2hub2RlLmNvbS9yZXMvaGFzaG5vZGUvaW1hZ2UvdXBsb2FkL3YxNzIxODgzNDY3MjAzL2Rqd3RnOUNIMy5qcGVnIiwicmVhZFRpbWUiOjZ9" height="400" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://rahulvijayvergiya.hashnode.dev/javascript-memory-management-the-secret-to-a-clean-and-fast-app" rel="noopener noreferrer" class="c-link"&gt;
          JavaScript Memory Management: The Secret to a Clean and Fast App
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          Understanding how JavaScript manages memory, including the role of garbage collection and memory leaks.
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1611242173172%2FAOX1gE2jc.png" width="32" height="32"&gt;
        rahulvijayvergiya.hashnode.dev
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;



</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
      <category>node</category>
    </item>
    <item>
      <title>Event Loop and Concurrency in JavaScript</title>
      <dc:creator>Rahul Vijayvergiya</dc:creator>
      <pubDate>Tue, 13 Aug 2024 18:30:00 +0000</pubDate>
      <link>https://dev.to/rahulvijayvergiya/event-loop-and-concurrency-in-javascript-3flf</link>
      <guid>https://dev.to/rahulvijayvergiya/event-loop-and-concurrency-in-javascript-3flf</guid>
      <description>&lt;p&gt;This post was initially published on my &lt;a href="https://rahulvijayvergiya.hashnode.dev" rel="noopener noreferrer"&gt;blog&lt;/a&gt;. Check out the original source using the link below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://rahulvijayvergiya.hashnode.dev/event-loop-and-concurrency-in-javascript" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Frahulvijayvergiya.hashnode.dev%2Fapi%2Fog%2Fpost%3Fog%3DeyJ0aXRsZSI6IkV2ZW50JTIwTG9vcCUyMGFuZCUyMENvbmN1cnJlbmN5JTIwaW4lMjBKYXZhU2NyaXB0IiwiYXV0aG9yIjoiUmFodWwlMjBWaWpheXZlcmdpeWEiLCJkb21haW4iOiJyYWh1bHZpamF5dmVyZ2l5YS5oYXNobm9kZS5kZXYiLCJwaG90byI6Imh0dHBzOi8vY2RuLmhhc2hub2RlLmNvbS9yZXMvaGFzaG5vZGUvaW1hZ2UvdXBsb2FkL3YxNzIxODgzNDY3MjAzL2Rqd3RnOUNIMy5qcGVnIiwicmVhZFRpbWUiOjR9" height="400" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://rahulvijayvergiya.hashnode.dev/event-loop-and-concurrency-in-javascript" rel="noopener noreferrer" class="c-link"&gt;
          Event Loop and Concurrency in JavaScript
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          This article explain how JavaScript handles asynchronous operations with the event loop, microtasks, and macrotasks.
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1611242173172%2FAOX1gE2jc.png" width="32" height="32"&gt;
        rahulvijayvergiya.hashnode.dev
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  What is the Event Loop?
&lt;/h2&gt;

&lt;p&gt;Event loop is the core mechanism of javascript that allows it to perform non-blocking operations, even though it’s single-threaded. It continuously checks the call stack and the task queue (or message queue) to check what should be executed next.&lt;/p&gt;

&lt;p&gt;Here's how the event loop operates:&lt;br&gt;
&lt;strong&gt;1. Call Stack&lt;/strong&gt;: JavaScript has a call stack that keeps track of function executions. When a function is called, it’s added to the stack. When it finishes executing, it’s removed from the stack.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Task Queue&lt;/strong&gt;: When an asynchronous operation completes (e.g., an API call, setTimeout), its callback is placed in the task queue.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Event Loop&lt;/strong&gt;: The event loop continuously monitors the call stack and the task queue. If the call stack is empty, the event loop will push the first task from the task queue into the call stack, allowing it to be executed.&lt;/p&gt;
&lt;h2&gt;
  
  
  Asynchronous Operations in JavaScript
&lt;/h2&gt;

&lt;p&gt;JavaScript uses different types of asynchronous operations, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Timers (setTimeout, setInterval)&lt;/strong&gt;: These functions execute code after a specified delay.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Promises&lt;/strong&gt;: Used for handling asynchronous operations, promises can be resolved or rejected and are managed by the microtask queue.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Event Listeners&lt;/strong&gt;: Events such as clicks, key presses, and network requests trigger asynchronous callbacks.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Macrotasks vs. Microtasks in Event loop
&lt;/h2&gt;

&lt;p&gt;To understand the event loop better, we need to distinguish between macrotasks and microtasks.&lt;/p&gt;
&lt;h3&gt;
  
  
  Macrotasks
&lt;/h3&gt;

&lt;p&gt;Macrotasks include operations like setTimeout, setInterval, I/O, and event callbacks. When these operations are complete, their callbacks are pushed into the task queue, waiting for the event loop to pick them up.&lt;/p&gt;
&lt;h3&gt;
  
  
  Microtasks
&lt;/h3&gt;

&lt;p&gt;Microtasks, on the other hand, are primarily related to promise resolutions and MutationObserver. When a microtask is queued, it is processed after the currently executing script and before any macrotasks. This gives microtasks higher priority over macrotasks.&lt;/p&gt;
&lt;h2&gt;
  
  
  How the Event Loop Handles Tasks
&lt;/h2&gt;

&lt;p&gt;Let’s break down the event loop with 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="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;Start&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="s1"&gt;Timeout&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;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="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;Promise&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="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;End&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;strong&gt;Output:&lt;/strong&gt;&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;Start&lt;/span&gt;
&lt;span class="nx"&gt;End&lt;/span&gt;
&lt;span class="nb"&gt;Promise&lt;/span&gt;
&lt;span class="nx"&gt;Timeout&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here’s what happens:&lt;/p&gt;

&lt;p&gt;1) The synchronous code is executed first:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;console.log('Start') is executed and logged.&lt;/li&gt;
&lt;li&gt;setTimeout is encountered, and its callback is queued as a macrotask.&lt;/li&gt;
&lt;li&gt;Promise.resolve().then(...) is encountered, and its callback is queued as a microtask.&lt;/li&gt;
&lt;li&gt;console.log('End') is executed and logged.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2) The call stack is now empty, so the event loop checks the microtask queue:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The promise’s .then() callback is executed, logging Promise.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;3) Only after the microtask queue is empty does the event loop move to the macrotask queue:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The setTimeout callback is executed, logging Timeout.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Understanding Event Loop as a Restaurant
&lt;/h2&gt;

&lt;p&gt;To make the concept of the event loop more easy to understand, let’s compare it to how a restaurant kitchen operates.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. The Call Stack as the Chef&lt;/strong&gt;: Imagine the chef is the call stack, responsible for cooking one dish at a time. When an order comes in, the chef starts preparing it. They can only work on one dish at a time, so if a new order comes in while they’re busy, it has to wait until the current dish is finished.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. The Task Queue as the Wait Staff&lt;/strong&gt;: The task queue is like the wait staff, who bring orders to the chef. They keep track of the orders waiting to be prepared. When the chef finishes a dish, the next order is passed to them to start cooking.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Microtasks as Quick Fixes&lt;/strong&gt;: Sometimes, the chef might need to do something really quick, like adding a garnish or tasting a sauce. These are like microtasks—small, quick jobs that get done before the chef moves on to the next big order.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;4. Macrotasks as Full Orders&lt;/strong&gt;: Full meal preparations, like cooking a steak or making a salad, represent macrotasks. These take more time and are handled one after the other.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;5. Event Loop as the Kitchen Manager&lt;/strong&gt;: The event loop is like the kitchen manager who oversees everything, ensuring the chef stays busy but not overwhelmed. They make sure the chef handles quick fixes (microtasks) before starting the next full order (macrotask).&lt;/p&gt;

&lt;h2&gt;
  
  
  Common Issues
&lt;/h2&gt;

&lt;p&gt;Understanding the event loop, microtasks, and macrotasks helps avoid common pitfalls in JavaScript, such as:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1) Starvation of macrotasks&lt;/strong&gt;: when macrotasks (like setTimeout, setInterval, and event callbacks) are delayed or don't get a chance to run because the event loop is constantly busy handling microtasks (like promise callbacks). In simpler terms, if your code keeps adding more and more microtasks, they can pile up and get processed continuously, leaving no time for the macrotasks to execute. This can cause certain actions, like a setTimeout callback, to be delayed or not run as expected, which can make your application feel slow or unresponsive.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2) UI responsiveness&lt;/strong&gt;: Since microtasks are executed before the rendering phase, heavy usage of microtasks can delay the UI update.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;br&gt;
The event loop, with its handling of microtasks and macrotasks gives JavaScript its concurrency capabilities. By managing the execution of async tasks efficiently, JavaScript ensures that web applications remain responsive and can handle multiple operations without blocking the main thread.&lt;/p&gt;

&lt;h2&gt;
  
  
  References
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Event_loop" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Event_loop&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://nodejs.org/en/learn/asynchronous-work/event-loop-timers-and-nexttick" rel="noopener noreferrer"&gt;https://nodejs.org/en/learn/asynchronous-work/event-loop-timers-and-nexttick&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.studysmarter.co.uk/explanations/computer-science/computer-programming/javascript-event-loop/" rel="noopener noreferrer"&gt;https://www.studysmarter.co.uk/explanations/computer-science/computer-programming/javascript-event-loop/&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.to/presto412/visualising-the-javascript-event-loop-with-a-pizza-restaurant-analogy-47a8"&gt;https://dev.to/presto412/visualising-the-javascript-event-loop-with-a-pizza-restaurant-analogy-47a8&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>How Web Workers Supercharge Web Application</title>
      <dc:creator>Rahul Vijayvergiya</dc:creator>
      <pubDate>Mon, 05 Aug 2024 07:07:15 +0000</pubDate>
      <link>https://dev.to/rahulvijayvergiya/how-web-workers-supercharge-web-application-4jbd</link>
      <guid>https://dev.to/rahulvijayvergiya/how-web-workers-supercharge-web-application-4jbd</guid>
      <description>&lt;p&gt;This post was initially published on my &lt;a href="https://rahulvijayvergiya.hashnode.dev" rel="noopener noreferrer"&gt;blog&lt;/a&gt;. Check out the original source using the link below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://rahulvijayvergiya.hashnode.dev/how-web-workers-supercharge-web-application" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Frahulvijayvergiya.hashnode.dev%2Fapi%2Fog%2Fpost%3Fog%3DeyJ0aXRsZSI6IkhvdyUyMFdlYiUyMFdvcmtlcnMlMjBTdXBlcmNoYXJnZSUyMFdlYiUyMEFwcGxpY2F0aW9uIiwiYXV0aG9yIjoiUmFodWwlMjBWaWpheXZlcmdpeWEiLCJkb21haW4iOiJyYWh1bHZpamF5dmVyZ2l5YS5oYXNobm9kZS5kZXYiLCJwaG90byI6Imh0dHBzOi8vY2RuLmhhc2hub2RlLmNvbS9yZXMvaGFzaG5vZGUvaW1hZ2UvdXBsb2FkL3YxNzIxODgzNDY3MjAzL2Rqd3RnOUNIMy5qcGVnIiwicmVhZFRpbWUiOjV9" height="400" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://rahulvijayvergiya.hashnode.dev/how-web-workers-supercharge-web-application" rel="noopener noreferrer" class="c-link"&gt;
          How Web Workers Supercharge Web Application
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          Workers is a powerful feature in JavaScript which allow background processing and giving us option to run scripts concurrently with the main thread.
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1611242173172%2FAOX1gE2jc.png" width="32" height="32"&gt;
        rahulvijayvergiya.hashnode.dev
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;In today's web development, performance is a critical when a application run on various devices like mobile phones, laptops, tablets, and iPads, each with different memory capabilities. As applications grows, handling multiple tasks concurrently without blocking the main thread becomes important. &lt;strong&gt;Web Workers&lt;/strong&gt; is a powerful feature in JavaScript which allow background processing and giving us option to run scripts concurrently with the main thread.&lt;/p&gt;

&lt;h3&gt;
  
  
  What are Web Workers?
&lt;/h3&gt;

&lt;p&gt;Web Workers are a standard feature of JavaScript that allow us to run scripts in the background which are independent of the main thread. Which means that tasks that takes a long time to execute can be offloaded to a worker, giving space for the main thread to handle user interactions, rendering, and other critical tasks of web application.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Web Workers?
&lt;/h3&gt;

&lt;p&gt;JavaScript is single-threaded, which means it can only do one thing at a time. If we have a long-running task, like processing a large dataset or rendering a complex graphic then it can block the main thread, as a result UI can lag or freeze.&lt;/p&gt;

&lt;h4&gt;
  
  
  Types of Web Workers
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Dedicated Workers:&lt;/strong&gt; Operate in a single context and communicate with the main thread via messages.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Shared Workers:&lt;/strong&gt; Can be accessed by multiple scripts, even across different windows, tabs, or iframes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Service Workers:&lt;/strong&gt; Act as a proxy between your web application and the network, managing push notifications, background sync, and caching resources.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  How Web Workers Work?
&lt;/h3&gt;

&lt;p&gt;Web Workers create a separate JavaScript file that the worker will execute. The main script and the worker script communicate using the &lt;code&gt;postMessage()&lt;/code&gt; and &lt;code&gt;onmessage&lt;/code&gt; methods. The worker runs in isolation, without access to the DOM or global variables from the main thread, ensuring safe, concurrent execution.&lt;/p&gt;

&lt;p&gt;Here’s a simple example of creating a Web Worker:&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;// main.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;worker&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;Worker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;worker.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onmessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&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;Message from worker:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;postMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello Worker!&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// worker.js&lt;/span&gt;
&lt;span class="nx"&gt;onmessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&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;Message from main thread:&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;postMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello from Worker!&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Some Real-World Use Cases of Web Workers
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;1. Process large CSV files&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Imagine we’re building a web application that allows users to upload and process large CSV files. This app parses the CSV, performs data analysis, and then displays the results. Without Web Workers, this processing would block the main thread.&lt;/p&gt;

&lt;p&gt;Here’s how we can use Web Workers to handle this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;Setting Up the Web Worker&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;First, we’ll create a separate JavaScript file, say &lt;code&gt;worker.js&lt;/code&gt;, which will handle the data processing.&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;// worker.js&lt;/span&gt;
&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onmessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&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;csvData&lt;/span&gt; &lt;span class="o"&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;data&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;processData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;csvData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Assume processData is a function that handles your data&lt;/span&gt;
    &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;postMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&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;processData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// complex data processing logic here&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;processedData&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;ol&gt;
&lt;li&gt;&lt;strong&gt;Using the Web Worker in Main Thread&lt;/strong&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Next, in your main JavaScript file, we’ll create a new worker instance and post the CSV data to it.&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;// main.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;worker&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;Worker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;worker.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onmessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&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;processedData&lt;/span&gt; &lt;span class="o"&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;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="nf"&gt;displayResults&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;processedData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Function to update the UI with the processed data&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;handleFileUpload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&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;reader&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;FileReader&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;reader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&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;csvData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;reader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nx"&gt;worker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;postMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;csvData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Send data to the worker&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="nx"&gt;reader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;readAsText&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, main thread reads the uploaded file and sends data to the Web Worker. Web Worker processes this data in the background and sends the results back once it’s done. &lt;strong&gt;Meanwhile, the main thread remains free to do other task in the UI, show progress indicators, or respond to other user input.&lt;/strong&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  2. &lt;strong&gt;Image Processing&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;Image processing is CPU-intensive tasks that can significantly impact performance of application, if its done on the main thread. Web Workers can handle tasks like resizing, filtering, or converting images in the background.&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;// imageWorker.js&lt;/span&gt;
&lt;span class="nx"&gt;onmessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&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;imageData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// Perform heavy image processing&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;processedImage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;applyFilter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;imageData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;postMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;processedImage&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// main.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;imageWorker&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;Worker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;imageWorker.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;imageWorker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;postMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;imageData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;imageWorker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onmessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;displayImage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&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;h4&gt;
  
  
  3. &lt;strong&gt;Data Processing and Parsing&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;if we need to deal with large dataset, such as parsing JSON files or processing large arrays then we can offload this to web workers. This is particularly useful in data-intensive applications like analytics dashboards or real-time monitoring systems.&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;// dataWorker.js&lt;/span&gt;
&lt;span class="nx"&gt;onmessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&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;largeDataset&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&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;processedData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;processData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;largeDataset&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;postMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;processedData&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// main.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;dataWorker&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;Worker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;dataWorker.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;dataWorker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;postMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;largeDataset&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;dataWorker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onmessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;sendUpdatedDataToUI&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&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;h4&gt;
  
  
  4. &lt;strong&gt;Complex Mathematical Calculations&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;if our application need complex mathematical operations, such as simulations, cryptography, or machine learning tasks then this can also be offloaded to web workers&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;// mathWorker.js&lt;/span&gt;
&lt;span class="nx"&gt;onmessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&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;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;performComplexCalculation&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;postMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&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;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;javascriptCopy&lt;/span&gt; &lt;span class="nx"&gt;code&lt;/span&gt;&lt;span class="c1"&gt;// main.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mathWorker&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;Worker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mathWorker.js&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;mathWorker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;postMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inputData&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;mathWorker&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onmessage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;displayCalculationResult&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&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;h3&gt;
  
  
  Limitations of Web Workers
&lt;/h3&gt;

&lt;p&gt;Web Workers come with certain limitations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;No DOM Access:&lt;/strong&gt; Web Workers cannot manipulate the DOM directly, so any UI updates need to be communicated back to the main thread.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Limited API Access:&lt;/strong&gt; Not all JavaScript APIs are available in Web Workers. For eg., you cannot use &lt;code&gt;localStorage&lt;/code&gt; or directly make XMLHttpRequests in older browsers.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Web Workers are a powerful tool in the a web developer’s toolkit, enabling him to build responsive, user-friendly applications. By offloading resource heavy (resource Hungary actually :) ) task to background threads, developer can keep his main thread free to handle user interactions and UI updates.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://web.dev/learn/performance/web-worker-overview" rel="noopener noreferrer"&gt;https://web.dev/learn/performance/web-worker-overview&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;[&lt;a href="https://www.w3schools.com/html/html5%5C_webworkers.asp" rel="noopener noreferrer"&gt;https://www.w3schools.com/html/html5\_webworkers.asp&lt;/a&gt;]&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>node</category>
      <category>react</category>
      <category>webdev</category>
    </item>
    <item>
      <title>JavaScript Generators</title>
      <dc:creator>Rahul Vijayvergiya</dc:creator>
      <pubDate>Mon, 29 Jul 2024 09:30:08 +0000</pubDate>
      <link>https://dev.to/rahulvijayvergiya/javascript-generators-17bb</link>
      <guid>https://dev.to/rahulvijayvergiya/javascript-generators-17bb</guid>
      <description>&lt;p&gt;This post was initially published on my &lt;a href="https://rahulvijayvergiya.hashnode.dev" rel="noopener noreferrer"&gt;blog&lt;/a&gt;. Check out the original source using the link below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://rahulvijayvergiya.hashnode.dev/javascript-generators" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Frahulvijayvergiya.hashnode.dev%2Fapi%2Fog%2Fpost%3Fog%3DeyJ0aXRsZSI6IkphdmFTY3JpcHQlMjBHZW5lcmF0b3JzIiwiYXV0aG9yIjoiUmFodWwlMjBWaWpheXZlcmdpeWEiLCJkb21haW4iOiJyYWh1bHZpamF5dmVyZ2l5YS5oYXNobm9kZS5kZXYiLCJwaG90byI6Imh0dHBzOi8vY2RuLmhhc2hub2RlLmNvbS9yZXMvaGFzaG5vZGUvaW1hZ2UvdXBsb2FkL3YxNzIxODgzNDY3MjAzL2Rqd3RnOUNIMy5qcGVnIiwicmVhZFRpbWUiOjR9" height="400" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://rahulvijayvergiya.hashnode.dev/javascript-generators" rel="noopener noreferrer" class="c-link"&gt;
          JavaScript Generators
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          Explore JavaScript generators, Learn how to create and use generators for managing control flow, handling asyn operations, and creating iterators
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1611242173172%2FAOX1gE2jc.png" width="32" height="32"&gt;
        rahulvijayvergiya.hashnode.dev
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;JavaScript generators are a powerful feature introduced in ECMAScript 6 (ES6) that provide a new way to work with functions and manage asynchronous operations. They enable developers to write code that can pause and resume its execution, offering greater control over the flow of data and execution.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Are Javascript Generators?
&lt;/h2&gt;

&lt;p&gt;Generators are a special type of function that can be paused and resumed, allowing you to control the execution flow more precisely. They are defined using the function* syntax and produce values using the yield keyword.&lt;/p&gt;

&lt;h2&gt;
  
  
  Syntax of Javascript Generators
&lt;/h2&gt;

&lt;p&gt;Here's a basic example of a generator function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function* generatorFunction() {
    yield 1;
    yield 2;
    yield 3;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;To use the generator function, you need to create an iterator:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const generator = generatorFunction();

console.log(generator.next()); // { value: 1, done: false }
console.log(generator.next()); // { value: 2, done: false }
console.log(generator.next()); // { value: 3, done: false }
console.log(generator.next()); // { value: undefined, done: true }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  How JavaScript Generators Work
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Creation: When a generator function is called, it does not execute its code immediately. Instead, it returns an iterator object.&lt;/li&gt;
&lt;li&gt;Execution: The code inside the generator function runs when the iterator's next() method is called.&lt;/li&gt;
&lt;li&gt;Yield: The yield keyword pauses the generator function and returns the value. The function can be resumed from where it left off when next() is called again.&lt;/li&gt;
&lt;li&gt;Completion: When the generator function completes its execution, the iterator returns an object with done: true.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Use Cases for JavaScript Generators
&lt;/h2&gt;

&lt;p&gt;Generators are versatile and can be used in various scenarios, such as:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Lazy Evaluation&lt;/strong&gt;: Generators allow you to generate values on-the-fly, which is useful for working with large datasets or streams of data.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function* infiniteSequence() {
     let i = 0;
     while (true) {
         yield i++;
     }
 }

 const iterator = infiniteSequence();
 console.log(iterator.next().value); // 0
 console.log(iterator.next().value); // 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;2. Asynchronous Programming&lt;/strong&gt;: Generators can be combined with Promises to handle asynchronous operations more elegantly.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function* fetchData() {
     const data1 = yield fetch('https://api.example.com/data1');
     const data2 = yield fetch('https://api.example.com/data2');
     return [data1, data2];
 }

 const iterator = fetchData();
 iterator.next().value.then(response1 =&amp;gt; {
     iterator.next(response1).value.then(response2 =&amp;gt; {
         console.log(iterator.next(response2).value);
     });
 });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;3. Iterating Over Collections&lt;/strong&gt;: Generators can create custom iterators for objects or arrays.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const collection = {
     items: [1, 2, 3],
     *[Symbol.iterator]() {
         for (const item of this.items) {
             yield item;
         }
     }
 };

 for (const item of collection) {
     console.log(item); // 1, 2, 3
 }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Advanced javascript Generator Concepts
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Generator Delegation&lt;/strong&gt;: Generators can delegate to another generator or iterable using the yield* expression.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function* subGenerator() {
     yield 'a';
     yield 'b';
 }

 function* mainGenerator() {
     yield* subGenerator();
     yield 'c';
 }

 const iterator = mainGenerator();
 console.log(iterator.next().value); // 'a'
 console.log(iterator.next().value); // 'b'
 console.log(iterator.next().value); // 'c'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;2. Two-way Communication&lt;/strong&gt;: You can send values back into the generator using the next method.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function* generatorFunction() {
     const result = yield 'Initial';
     console.log(result); // 'Input'
 }

 const iterator = generatorFunction();
 console.log(iterator.next().value); // 'Initial'
 iterator.next('Input');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;3. Error Handling&lt;/strong&gt;: Generators can handle errors gracefully using try...catch blocks.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function* generatorFunction() {
     try {
         yield 'Try this';
         throw new Error('Something went wrong');
     } catch (error) {
         console.log(error.message); // 'Something went wrong'
     }
 }

 const iterator = generatorFunction();
 console.log(iterator.next().value); // 'Try this'
 iterator.next();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;4. Pausing and Resuming Execution&lt;/strong&gt;: Generators can be paused and resumed multiple times, making them useful for complex state management.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function* stateMachine() {
    const state1 = yield 'State 1';
    console.log(state1); // 'State A'
    const state2 = yield 'State 2';
    console.log(state2); // 'State B'
}

const iterator = stateMachine();
console.log(iterator.next().value); // 'State 1'
console.log(iterator.next('State A').value); // 'State 2'
iterator.next('State B');
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;5. Using Generators for Stream Processing&lt;/strong&gt;: Generators can be particularly useful for processing streams of data, allowing you to handle each chunk of data as it arrives.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const fs = require('fs');
const readline = require('readline');

function* processFile(filename) {
   const fileStream = fs.createReadStream(filename);
   const rl = readline.createInterface({
       input: fileStream,
       crlfDelay: Infinity
   });

   for await (const line of rl) {
       yield line;
   }
}

(async () =&amp;gt; {
   for await (const line of processFile('example.txt')) {
       console.log(`Line from file: ${line}`);
   }
})();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

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

&lt;p&gt;JavaScript Generators are a powerful and flexible feature in JavaScript, providing a new way to manage control flow and handle asynchronous operations. By understanding how to create and use generators, you can write more efficient and readable code. Whether you're working with large data sets, implementing lazy evaluation, or managing asynchronous tasks, generators can significantly enhance your JavaScript programming experience.&lt;/p&gt;

&lt;p&gt;This post was initially published on my &lt;a href="https://rahulvijayvergiya.hashnode.dev" rel="noopener noreferrer"&gt;blog&lt;/a&gt;. Check out the original source using the link below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://rahulvijayvergiya.hashnode.dev/javascript-generators" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Frahulvijayvergiya.hashnode.dev%2Fapi%2Fog%2Fpost%3Fog%3DeyJ0aXRsZSI6IkphdmFTY3JpcHQlMjBHZW5lcmF0b3JzIiwiYXV0aG9yIjoiUmFodWwlMjBWaWpheXZlcmdpeWEiLCJkb21haW4iOiJyYWh1bHZpamF5dmVyZ2l5YS5oYXNobm9kZS5kZXYiLCJwaG90byI6Imh0dHBzOi8vY2RuLmhhc2hub2RlLmNvbS9yZXMvaGFzaG5vZGUvaW1hZ2UvdXBsb2FkL3YxNzIxODgzNDY3MjAzL2Rqd3RnOUNIMy5qcGVnIiwicmVhZFRpbWUiOjR9" height="400" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://rahulvijayvergiya.hashnode.dev/javascript-generators" rel="noopener noreferrer" class="c-link"&gt;
          JavaScript Generators
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          Explore JavaScript generators, Learn how to create and use generators for managing control flow, handling asyn operations, and creating iterators
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1611242173172%2FAOX1gE2jc.png" width="32" height="32"&gt;
        rahulvijayvergiya.hashnode.dev
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;



</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>react</category>
      <category>node</category>
    </item>
    <item>
      <title>Understanding JavaScript Promises</title>
      <dc:creator>Rahul Vijayvergiya</dc:creator>
      <pubDate>Mon, 29 Jul 2024 04:24:52 +0000</pubDate>
      <link>https://dev.to/rahulvijayvergiya/understanding-javascript-promises-29j4</link>
      <guid>https://dev.to/rahulvijayvergiya/understanding-javascript-promises-29j4</guid>
      <description>&lt;p&gt;This post was initially published on my &lt;a href="https://rahulvijayvergiya.hashnode.dev" rel="noopener noreferrer"&gt;blog&lt;/a&gt;. Check out the original source using the link below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://rahulvijayvergiya.hashnode.dev/understanding-javascript-promises#heading-handling-multiple-promises" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Frahulvijayvergiya.hashnode.dev%2Fapi%2Fog%2Fpost%3Fog%3DeyJ0aXRsZSI6IlVuZGVyc3RhbmRpbmclMjBKYXZhU2NyaXB0JTIwUHJvbWlzZXMiLCJhdXRob3IiOiJSYWh1bCUyMFZpamF5dmVyZ2l5YSIsImRvbWFpbiI6InJhaHVsdmlqYXl2ZXJnaXlhLmhhc2hub2RlLmRldiIsInBob3RvIjoiaHR0cHM6Ly9jZG4uaGFzaG5vZGUuY29tL3Jlcy9oYXNobm9kZS9pbWFnZS91cGxvYWQvdjE3MjE4ODM0NjcyMDMvZGp3dGc5Q0gzLmpwZWciLCJyZWFkVGltZSI6NCwicmVhY3Rpb25zIjoxfQ%3D%3D" height="400" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://rahulvijayvergiya.hashnode.dev/understanding-javascript-promises#heading-handling-multiple-promises" rel="noopener noreferrer" class="c-link"&gt;
          
Understanding JavaScript Promises
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          Learn the essentials of JavaScript promises, including their creation, usage, and common pitfalls. Understand how to handle asynchronous operations.
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1611242173172%2FAOX1gE2jc.png" width="32" height="32"&gt;
        rahulvijayvergiya.hashnode.dev
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;


&lt;p&gt;&lt;strong&gt;JavaScript promises&lt;/strong&gt; are a powerful way to handle asynchronous operations. Before the introduction of promises, callbacks were the primary method to handle asynchronous tasks. However, callbacks can lead to "callback hell," where nested callbacks become difficult to manage and read. Promises provide a cleaner, more readable way to manage asynchronous operations.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is a Promise?
&lt;/h2&gt;

&lt;p&gt;A promise in JavaScript represents the eventual completion (or failure) of an asynchronous operation and its resulting value. It is an object that can be in one of three states:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pending: Initial state, neither fulfilled nor rejected.&lt;/li&gt;
&lt;li&gt;Fulfilled: Operation completed successfully.&lt;/li&gt;
&lt;li&gt;Rejected: Operation failed.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Creating a Promise
&lt;/h2&gt;

&lt;p&gt;To create a promise, use the Promise constructor, which takes a function with two parameters: resolve and reject. These functions control the promise's state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const myPromise = new Promise((resolve, reject) =&amp;gt; {
  let success = true; // Simulate an operation
  if (success) {
    resolve("Operation successful!");
  } else {
    reject("Operation failed.");
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Using Promises
&lt;/h2&gt;

&lt;p&gt;Promises have then, catch, and finally methods to handle the result.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;then: Called when the promise is fulfilled.&lt;/li&gt;
&lt;li&gt;catch: Called when the promise is rejected.&lt;/li&gt;
&lt;li&gt;finally: Called regardless of the promise's outcome.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;myPromise
  .then((message) =&amp;gt; {
    console.log(message); // "Operation successful!"
  })
  .catch((error) =&amp;gt; {
    console.error(error); // "Operation failed."
  })
  .finally(() =&amp;gt; {
    console.log("Operation completed.");
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Chaining Promises
&lt;/h2&gt;

&lt;p&gt;One of the main advantages of promises is chaining. You can chain multiple then calls to perform a sequence of asynchronous operations.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const firstPromise = new Promise((resolve) =&amp;gt; {
  setTimeout(() =&amp;gt; resolve("First promise resolved"), 1000);
});

firstPromise
  .then((result) =&amp;gt; {
    console.log(result); // "First promise resolved"
    return new Promise((resolve) =&amp;gt; {
      setTimeout(() =&amp;gt; resolve("Second promise resolved"), 1000);
    });
  })
  .then((result) =&amp;gt; {
    console.log(result); // "Second promise resolved"
  })
  .catch((error) =&amp;gt; {
    console.error(error);
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Handling Multiple Promises
&lt;/h2&gt;

&lt;p&gt;JavaScript provides utility methods to handle multiple promises concurrently: Promise.all, Promise.race, Promise.allSettled, and Promise.any.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Promise.all: Waits for all promises to resolve or any to reject.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const promise1 = Promise.resolve(3);
const promise2 = 42;
const promise3 = new Promise((resolve) =&amp;gt; setTimeout(resolve, 100, 'foo'));

Promise.all([promise1, promise2, promise3]).then((values) =&amp;gt; {
  console.log(values); // [3, 42, 'foo']
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Promise.race: Resolves or rejects as soon as one of the promises resolves or rejects.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const promise1 = new Promise((resolve) =&amp;gt; setTimeout(resolve, 500, 'one'));
const promise2 = new Promise((resolve) =&amp;gt; setTimeout(resolve, 100, 'two'));

Promise.race([promise1, promise2]).then((value) =&amp;gt; {
  console.log(value); // "two"
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Promise.allSettled: Waits for all promises to settle (either resolve or reject).
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const promise1 = Promise.resolve(42);
const promise2 = Promise.reject('error');
const promise3 = new Promise((resolve) =&amp;gt; setTimeout(resolve, 100, 'foo'));

Promise.allSettled([promise1, promise2, promise3]).then((results) =&amp;gt; {
  results.forEach((result) =&amp;gt; console.log(result.status));
  // "fulfilled"
  // "rejected"
  // "fulfilled"
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;ul&gt;
&lt;li&gt;Promise.any: Waits for any promise to resolve.
&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const promise1 = Promise.reject('error1');
const promise2 = new Promise((resolve) =&amp;gt; setTimeout(resolve, 100, 'foo'));

Promise.any([promise1, promise2]).then((value) =&amp;gt; {
  console.log(value); // "foo"
}).catch((error) =&amp;gt; {
  console.error(error); // AggregateError: All promises were rejected
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h2&gt;
  
  
  Common Mistakes with Promises
&lt;/h2&gt;

&lt;p&gt;While promises are powerful, they can also be a source of confusion and errors. Here are some common mistakes developers make when working with promises:&lt;/p&gt;
&lt;h3&gt;
  
  
  1. Not Returning a Promise in a then Handler
&lt;/h3&gt;

&lt;p&gt;For chaining to work correctly, you must return a promise inside a then handler. Failing to do so breaks the chain.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;firstPromise
  .then((result) =&amp;gt; {
    console.log(result);
    new Promise((resolve) =&amp;gt; setTimeout(resolve, 1000, 'Second promise resolved')); // Missing return
  })
  .then((result) =&amp;gt; {
    console.log(result); // This will log undefined
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  2. Catching Errors Too Early
&lt;/h3&gt;

&lt;p&gt;Catching an error early in a chain can prevent downstream then handlers from being aware of the error.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;firstPromise
  .then((result) =&amp;gt; {
    throw new Error('Something went wrong');
  })
  .catch((error) =&amp;gt; {
    console.error(error); // Caught too early
  })
  .then(() =&amp;gt; {
    console.log('This will still run');
  });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;To avoid this, place error handling at the end of the chain or as close to the specific operation as possible.&lt;/p&gt;
&lt;h3&gt;
  
  
  3. Creating Unnecessary Promises
&lt;/h3&gt;

&lt;p&gt;It's common to wrap synchronous operations in promises unnecessarily, which adds complexity.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const promise = new Promise((resolve) =&amp;gt; {
  resolve('Hello, World!');
});
promise.then((message) =&amp;gt; console.log(message));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Instead, just use the value directly:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Promise.resolve('Hello, World!').then((message) =&amp;gt; console.log(message));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  4. Forgetting to Handle Rejections
&lt;/h3&gt;

&lt;p&gt;Always handle rejections to prevent unhandled promise rejections, which can cause issues in your application.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const promise = new Promise((_, reject) =&amp;gt; {
  reject('Error occurred');
});
promise.then((message) =&amp;gt; console.log(message)); // No catch handler
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Always add a catch block:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;promise
  .then((message) =&amp;gt; console.log(message))
  .catch((error) =&amp;gt; console.error(error));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;h3&gt;
  
  
  5. Mixing Promises and Async/Await
&lt;/h3&gt;

&lt;p&gt;While promises and async/await are both ways to handle asynchronous code, mixing them can lead to confusion.&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function fetchData() {
  const data = await fetch('https://api.example.com/data');
  data.then((result) =&amp;gt; console.log(result)); // Unnecessary promise
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;Stick to one approach:&lt;br&gt;
&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;async function fetchData() {
  const data = await fetch('https://api.example.com/data');
  console.log(await data.json());
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

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

&lt;p&gt;Promises are an essential part of modern JavaScript, providing a robust way to handle asynchronous operations. By understanding how to create, use, and manage promises, you can write cleaner, more maintainable code. Avoiding common mistakes will help you leverage promises effectively and prevent potential pitfalls. Whether you're handling a single asynchronous task or coordinating multiple concurrent operations, promises are a powerful tool in your JavaScript toolkit.&lt;/p&gt;

&lt;p&gt;This post was initially published on my &lt;a href="https://rahulvijayvergiya.hashnode.dev" rel="noopener noreferrer"&gt;blog&lt;/a&gt;. Check out the original source using the link below:&lt;br&gt;
&lt;/p&gt;
&lt;div class="crayons-card c-embed text-styles text-styles--secondary"&gt;
      &lt;div class="c-embed__cover"&gt;
        &lt;a href="https://rahulvijayvergiya.hashnode.dev/understanding-javascript-promises#heading-handling-multiple-promises" class="c-link s:max-w-50 align-middle" rel="noopener noreferrer"&gt;
          &lt;img alt="" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Frahulvijayvergiya.hashnode.dev%2Fapi%2Fog%2Fpost%3Fog%3DeyJ0aXRsZSI6IlVuZGVyc3RhbmRpbmclMjBKYXZhU2NyaXB0JTIwUHJvbWlzZXMiLCJhdXRob3IiOiJSYWh1bCUyMFZpamF5dmVyZ2l5YSIsImRvbWFpbiI6InJhaHVsdmlqYXl2ZXJnaXlhLmhhc2hub2RlLmRldiIsInBob3RvIjoiaHR0cHM6Ly9jZG4uaGFzaG5vZGUuY29tL3Jlcy9oYXNobm9kZS9pbWFnZS91cGxvYWQvdjE3MjE4ODM0NjcyMDMvZGp3dGc5Q0gzLmpwZWciLCJyZWFkVGltZSI6NCwicmVhY3Rpb25zIjoxfQ%3D%3D" height="400" class="m-0" width="800"&gt;
        &lt;/a&gt;
      &lt;/div&gt;
    &lt;div class="c-embed__body"&gt;
      &lt;h2 class="fs-xl lh-tight"&gt;
        &lt;a href="https://rahulvijayvergiya.hashnode.dev/understanding-javascript-promises#heading-handling-multiple-promises" rel="noopener noreferrer" class="c-link"&gt;
          
Understanding JavaScript Promises
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;p class="truncate-at-3"&gt;
          Learn the essentials of JavaScript promises, including their creation, usage, and common pitfalls. Understand how to handle asynchronous operations.
        &lt;/p&gt;
      &lt;div class="color-secondary fs-s flex items-center"&gt;
          &lt;img alt="favicon" class="c-embed__favicon m-0 mr-2 radius-0" src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.hashnode.com%2Fres%2Fhashnode%2Fimage%2Fupload%2Fv1611242173172%2FAOX1gE2jc.png" width="32" height="32"&gt;
        rahulvijayvergiya.hashnode.dev
      &lt;/div&gt;
    &lt;/div&gt;
&lt;/div&gt;



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