simone-sanfratello / node-bench-iteration

What's the fastest way to iterate in node.js?
17 stars 2 forks source link

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


Stage 1

Array and Object

Questions

Cases

array

object

Report

array

object


Stage 2

Questions

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