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
.
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]
}
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 usingObject.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.
Array and Object
- 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 thanlet index
, butlet index
is a little bit faster with large arrays. To see in stage 2.
- Not that much. Looks like
- 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
and15
have the same kind of performance (although12
is slower).
- No. Version
- Does using
var
orlet
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.
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