Skip to content

What's the fastest way to iterate in node.js?

Notifications You must be signed in to change notification settings

simone-sanfratello/node-bench-iteration

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

What's the fastest way to iterate in node.js?

TLDR

report

Array

The "classic" for loop

for (let index = 0; index < array.length; index++) {
  const element = array[index]
}

Benchmarks say this is the fastest way and there is no relevant difference in variants like var index or const n = array.lenght ... index < n or ++index.

Object

Fastest way to iterate an object is using keys from Object.key

const keys = Object.keys(object)
for (let i = 0; i < keys.length; i++) {
  const key = keys[i]
  const value = object[key]
}

Insights

Facts seen running tests

  • Running order affects result, I guess because of garbage collector. So cases are shuffled every time and every scenario runs 10 times.
    Run a single test may lead to wrong results.
  • I started this project using benchmark, but I switched to benny because results are more consistent.
  • for in is always the slowest way to iterate an array (and also may introduce prototype pollution vulnerability).
  • for in is good to iterate an object but it also introduces prototype pollution vulnerability, and it is a way slower than using Object.keys.
  • benchmarks results on my laptop are influenced by laptop activity so I ran them in a VPS, to get the more consistent results possible.
    Of course all the test ran on the same machine.
  • while loop is excluded on purpose.

Stage 1

Array and Object

Questions

  • Does size affect iteration? Is there a way that perform better than another depending on array size?
    • Not that much. Looks like var index is generally slightly faster than let index, but let index is a little bit faster with large arrays. To see in stage 2.
  • Does content affect iteration?
    • No, results are linear. Of course simplest types are faster (to copy, in the examples) than complex ones.
  • Does node.js version affect iteration? Using a way or another has different perfomance depending by node version?
    • No. Version 14 (current LTS), 12 and 15 have the same kind of performance (although 12 is slower).

Cases

array

object

Report

array

object


Stage 2

Questions

  • Does using var or let affects performance?
    • No. Looks like it's all the same. Considering error margin, all the solution are equivalent.
  • Does incrementing by ++i affects performance?
    • No, same as above.

Cases

array

Report

array


Run test

node bench array1
# will take ~5h
node report array1

node bench array2
# will take ~4h
node report array2

node bench object
# will take ~1h
node report object

run single test

# node iterate [subject] [size] [type] [#run]

node iterate array1 100 STRING 01

subject: array1|array2|object
size: 10|100|1000|10000|100000|1000000
type: STRING|NUMBER|DATE|OBJECT|MIX
run: free label

About

What's the fastest way to iterate in node.js?

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published