<?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: Tanishq510</title>
    <description>The latest articles on DEV Community by Tanishq510 (@tanishq510).</description>
    <link>https://dev.to/tanishq510</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%2F431073%2Ff34df2f2-7649-4cb6-908b-a546e8cf4a3d.jpg</url>
      <title>DEV Community: Tanishq510</title>
      <link>https://dev.to/tanishq510</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tanishq510"/>
    <language>en</language>
    <item>
      <title>Promises, Async, and Speed: The JavaScript Cheatcode Your Senior Won’t Share! 🚀</title>
      <dc:creator>Tanishq510</dc:creator>
      <pubDate>Tue, 21 Jan 2025 04:55:06 +0000</pubDate>
      <link>https://dev.to/tanishq510/promises-async-and-speed-the-javascript-cheatcode-your-senior-wont-share-5c4o</link>
      <guid>https://dev.to/tanishq510/promises-async-and-speed-the-javascript-cheatcode-your-senior-wont-share-5c4o</guid>
      <description>&lt;p&gt;&lt;strong&gt;"Why this list render is taking so long??"&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Hey Devs,&lt;br&gt;
If you are relying too much on await for your async calls, then it's time to think if you are trading your app performance.&lt;/p&gt;

&lt;p&gt;Observe this code below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const result1 = await asyncFunction1(); // 1 second
const result2 = await asyncFunction2();// 1 second
const result3 = await asyncFunction3();// 1 second

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

&lt;/div&gt;



&lt;p&gt;The code looks okay for the first time, but the code execution is getting stopped for 1 second at each await.And going to take 3 seconds to process the code block&lt;/p&gt;

&lt;p&gt;This particular code block is going to take 3 seconds to complete. But what if we can process all async calls in one go??&lt;/p&gt;

&lt;h2&gt;
  
  
  Promise.all()
&lt;/h2&gt;

&lt;p&gt;The above code block can be performed simultaneously using &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all" rel="noopener noreferrer"&gt;Promise.all()&lt;/a&gt; method!!&lt;br&gt;
And the execution time can be reduced down to 1 second (Since all the executions are being performed simultaneously)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const results = await Promise.all([asyncFunction1(), asyncFunction2(), asyncFunction3()]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Promise.all() in Action
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Create three async functions using setTimeout

function asyncFunction1() {
  return new Promise((resolve) =&amp;gt; {
    setTimeout(() =&amp;gt; {
      console.log("output----1");
      resolve(1); // Return 1
    }, 1000); // 1 second delay
  });
}

function asyncFunction2() {
  return new Promise((resolve) =&amp;gt; {
    setTimeout(() =&amp;gt; {
      console.log("output----2");
      resolve(2); // Return 2
    }, 2000); // 2 second delay
  });
}

function asyncFunction3() {
  return new Promise((resolve) =&amp;gt; {
    setTimeout(() =&amp;gt; {
      console.log("output----3");
      resolve(3); // Return 3
    }, 3000); // 3 second delay
  });
}

// Call the functions and measure execution time
async function executeAsyncFunctions() {
  console.time("Execution Time (Sequential)");
  const startTime = Date.now();

  const result1 = await asyncFunction1();
  const result2 = await asyncFunction2();
  const result3 = await asyncFunction3();

  const endTime = Date.now();
  console.timeEnd("Execution Time (Sequential)");

  console.log("Time Taken (Sequential):", endTime - startTime, "ms");
  console.log("Results:", result1, result2, result3);
}

async function executeAsyncFunctionsConcurrently() {
  console.time("Execution Time (Concurrent)");
  const startTime = Date.now();

  const results = await Promise.all([asyncFunction1(), asyncFunction2(), asyncFunction3()]);

  const endTime = Date.now();
  console.timeEnd("Execution Time (Concurrent)");

  console.log("Time Taken (Concurrent):", endTime - startTime, "ms");
  console.log("Results:", results);
}

executeAsyncFunctions();
executeAsyncFunctionsConcurrently();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Observations!!
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Sequential Execution (executeAsyncFunctions)&lt;/strong&gt;&lt;br&gt;
The above code block has &lt;em&gt;executeAsyncFunctions&lt;/em&gt; all the async calls and it will take 7699 ms to process&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%2Fcmsoyrycktycu95yaa5o.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%2Fcmsoyrycktycu95yaa5o.png" alt="Code execution with await" width="682" height="282"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Concurrent Execution (executeAsyncFunctionsConcurrently)&lt;/strong&gt;&lt;br&gt;
On the other hand &lt;em&gt;executeAsyncFunctionsConcurrently&lt;/em&gt; will execute the code concurrently and will finish in comparatively less time&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%2Flwl329i543i8mq6uid2o.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%2Flwl329i543i8mq6uid2o.png" alt="Code execution with Promise" width="738" height="360"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;Promise.all() can be a saver for your JS application,specially when your code has multiple asynchronous calls.&lt;br&gt;
&lt;strong&gt;await&lt;/strong&gt; is good if you have one API call, but when dealing with &lt;strong&gt;multiple API calls&lt;/strong&gt; Promise.all() is your goto guy for performance optimisation. &lt;/p&gt;

&lt;h2&gt;
  
  
  Homework
&lt;/h2&gt;

&lt;p&gt;Promise Object comes with really helpful APIs,try to get yourself familiar with atleast the following!!&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Promise.all()&lt;/li&gt;
&lt;li&gt;Promise.allSettled()&lt;/li&gt;
&lt;li&gt;Promise.race()&lt;/li&gt;
&lt;li&gt;Promise.any()&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;"A Promise walked into a bar… but never settled the bill." 🍺&lt;/p&gt;
&lt;/blockquote&gt;

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