nested-loops-vs-function-call (v2)

Revision 2 of this benchmark created on


Setup

const colSize = 20_000;
const colCount = 5_000;
const toFind = `${Math.floor(Math.random() * (colCount/4))+ Math.floor(colCount*0.75)}-${Math.floor(Math.random() * (colSize/4)) + Math.floor(colSize*0.75)}`;
console.log(toFind)

function build() {
    return Array.from({length:colCount}).reduce((agg, _, idx) => {

        agg.push({items:
            Array.from({length:colSize}).reduce((iagg, _, iidx) => {
                iagg.push(`${idx}-${iidx}`);
                return iagg;
            },[])}
        );
        return agg;
    }, []);
}
const builtCollections = build();
function findCollectibleInCollections(
  collections,
  id,
) {
  for (const collection of collections ?? []) {
    for (const item of collection.items) {
      if (item === id) {
        return item;
      }
    }
  }
}

Test runner

Ready to run.

Testing in
TestOps/sec
function

	let found = undefined;
found = findCollectibleInCollections(builtCollections, toFind);

if(found) {
  console.log(`function found ${found}`);
  found = undefined;
}


ready
straight loop

let found = undefined;
loop:   for (const collection of builtCollections ?? []) {
    for (const item of collection.items) {
      if (item === toFind) {
      	found = item;
        break loop;
      }
    }
  }
  
if(found) {
  console.log(`loop found ${found}`);
}
ready

Revisions

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