Loops in NodeJs — Performance Optimization

Loops plays a vital role in an application performance. Most commonly used loops, execution impact is measured with huge sample of data.

Danish Siddiq
tajawal
6 min readMay 30, 2018

--

A few days back, a discussion happened in node backend team regarding scalable applications performance in Node.js. No one had doubt about node speed but application architecture and code execution are equally important. A well-designed system and clean code play a vital role in an application execution. Therefore I chose to write something from code area to help improving code execution time.

Loops are an essential element of code which is required by developers in many situations. I opted to discuss loops execution performance in detail with statistics to identify loop efficiency under different conditions and environments.

Efficiency is not just required for skyrocketing fuel prices

Iteration over collections is a constitutive ingredient in coding, plus if data involves complex structures then nested loops are inevitable. Therefore we will test different loops performances based on time taken by every loop on specific sample of data.

We will test loops performances on single dimensional and jagged arrays(array of arrays), every loop will run multiple times to observe average time taken under different environments, machines, and operating systems. Let’s give a fair chance to everyone.

One Dimensional Array (10 million iterations):

An array was filled with 10 million numbers in sequence, this structure will be traversed multiple times by different loops, the average will be calculated based on the time taken by a loop. Sample code for populating structure and different loops syntax used in testing are as below. Kindly note, each loop execute same instruction inside body which is not mentioned here to keep sample clean for understanding.

Following numbers were generated in milliseconds in different environments:

Stats are quite interesting on a different platform but classical loops showed better performance on data in comparison to Foreach and ES6. A picture is worth a thousand words so better to evaluate numbers in a graph:

Reverse-while is leading a sprint but definitely, reverse-while cannot be used in many circumstances. For loop, performance is impressive with marginal difference but before jumping to conclusion, let’s try same loops on jagged arrays.

Two Dimensional Array (100 million iterations):

A collection setup with 10,000 items and every item further contains 10,000 child’s so to visit each item in loop, it requires 10,000 * 10,000 = 100,000,000 (100 million iterations). Sample code for populating structure and different loops syntax used in testing are as below:

Following numbers were generated in milliseconds:

A comparison shows that once again, classical vanilla loops performed better. Results above were quite surprising for a jagged array as reverse while behaves little slower with nested structure in comparison to single dimension array, therefore it is advisable to evaluate structure according to the situation. Let’s compare loops in a bar chart:

One can easily make a decision that classical loops are far better in efficiency but if the structure does not contain bulk data then ES6 or forEach can opt for friendly syntax nature and subjectively more maintainable code but if speed is an objective then classical loops are not marginally but above par.

A logical reason for being slow:

In case of classical loops, engine mostly needs to worry about the length, increment, and termination but on the other side, functional setup and cleaning stuff after work done is a major overhead.

High-level languages engines are implemented with different tradeoffs which are dependent on multiple factors like performance, speed, easiness, maintenance etc that plays a vital role in code execution. Stack Overflow has very nice post specific to this topic which will further justify a reason for being slower.

Final findings (also from multiple articles):

  1. Classical loops are much better in performance but if an array has limited data and simple manipulations are required then alternate doesn’t sound bad for code maintenance purpose.
  2. Loops can perform differently under circumstances, therefore observe performance wisely according to requirements.
  3. Assigning array length in a different variable in an array statement brings the negligible difference in Node.js.
  4. Classical loops are not only better in performance but also avoid errors which are possible because of javascript flexibility. Stack Overflow has a very nice answer as to when to avoid and when to use for … in loop.
  5. Always observe code performance in different environments. Nothing is perfect except Creator, there is always room for improvisation.

Hit me up for any finding to share with geeks. Further code and structural areas will be explored in upcoming posts, stick around with us.

References:

--

--