<?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: Faheem 😒</title>
    <description>The latest articles on DEV Community by Faheem 😒 (@faheem).</description>
    <link>https://dev.to/faheem</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%2F156152%2Fba2b1cd8-f544-4840-9160-452f7efd7cfa.jpg</url>
      <title>DEV Community: Faheem 😒</title>
      <link>https://dev.to/faheem</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/faheem"/>
    <language>en</language>
    <item>
      <title>Saw a badly written NodeJS code causing performance issues</title>
      <dc:creator>Faheem 😒</dc:creator>
      <pubDate>Sat, 23 May 2020 06:35:04 +0000</pubDate>
      <link>https://dev.to/faheem/saw-a-badly-written-nodejs-code-causing-performance-issues-358k</link>
      <guid>https://dev.to/faheem/saw-a-badly-written-nodejs-code-causing-performance-issues-358k</guid>
      <description>&lt;p&gt;Note: I did not solve the issue. Sharing because I think it may be interesting.&lt;/p&gt;

&lt;h2&gt;
  
  
  Story
&lt;/h2&gt;

&lt;p&gt;Some weeks ago someone contacted me to fix a performance issue. They were getting a response time of around a second on their production server, and sometimes, it worked fine.&lt;/p&gt;

&lt;p&gt;The code worked perfectly on the local machine.&lt;/p&gt;

&lt;p&gt;I took a look at the code and turns out it is badly written and has a callback hell. &lt;b&gt;20 levels of nested database calls&lt;b&gt; in one place. &lt;/b&gt;&lt;/b&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Possible Cause:
&lt;/h2&gt;

&lt;p&gt;When you make calls to any services, such as the DB, it takes time. On localhost, since the database is on the same machine, the latency is almost 0.&lt;/p&gt;

&lt;p&gt;However, on a production server, each database call will have a higher latency, which depends on the configuration.&lt;/p&gt;

&lt;p&gt;Even if a single call has a latency of 50 ms (just an example), it will take 1000 ms (a second) for 20 DB calls. &lt;/p&gt;

&lt;p&gt;And since all these calls are nested (one after another), they are not benefiting from the async nature of NodeJS.&lt;/p&gt;

&lt;h2&gt;
  
  
  Possible Solution:
&lt;/h2&gt;

&lt;p&gt;While I did not solve the issue, I would have been solved the issue by:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Getting rid of the callback hell. &lt;/li&gt;
&lt;li&gt;Avoiding unnecessary DB calls. &lt;/li&gt;
&lt;li&gt;Diving the code in smaller functions, so they can work independently. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Batch updates and caching the data may help improve the performance too.&lt;/p&gt;

&lt;p&gt;Personally, the code reminded me of old-time when I was an intern and was making and solving the same mistakes. 😅&lt;/p&gt;

</description>
      <category>node</category>
      <category>performance</category>
      <category>callbackhell</category>
    </item>
  </channel>
</rss>
