Nested loops vs map

Benchmark created on


Description

TEST CASE: loop over the arrays and for each item, find the next item wich is twice as big

Setup

const bigArray = new Array(10_000).map((_, i) => i);
const smallArray = new Array(10).map((_, i) => i);

Test runner

Ready to run.

Testing in
TestOps/sec
Nested loops (BIG)
for (item in bigArray) {
    const doubleItem = bigArray.find(_item => _item === item * 2);
}
ready
Nested loops (SMALL)
for (item in smallArray) {
    const doubleItem = smallArray.find(_item => _item === item * 2);
}
ready
Map (BIG)
const bigArrayMap = bigArray.reduce((acc, item) => {
  acc[item] = item;
  return acc;
}, {});

for (item in bigArray) {
    const doubleItem = bigArrayMap[item * 2]
}
ready
Map (SMALL)
const smallArrayMap = smallArray.reduce((acc, item) => {
  acc[item * 2] = item;
  return acc;
}, {});

for (item in smallArray) {
    const doubleItem = smallArrayMap[item * 2]
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.