Loop performance (v10)

Revision 10 of this benchmark created on


Setup

var objectsArray;
var randomIdsArray;

 // Generate array of objects with id
  objectsArray = Array.from({ length: 1000000 }, (_, i) => ({
    id: `id-${i}`,
    value: Math.random(),
  }));

  // Create randomised array of ids
  randomIdsArray = objectsArray
    .map((obj) => obj.id)
    .sort(() => Math.random() - 0.5);

Teardown

  objectsArray = null;
  randomIdsArray = null;

Test runner

Ready to run.

Testing in
TestOps/sec
object
  const objStore = {};

  for (const item of objectsArray) {
    objStore[item.id] = item;
  }

  for (const id of randomIdsArray) {
    const item = objStore[id];
    if (!item) throw new Error(`Missing item for id ${id}`);
  }
ready
map
const mapStore = new Map();

  for (const item of objectsArray) {
    mapStore.set(item.id, item);
  }

  for (const id of randomIdsArray) {
    const item = mapStore.get(id);
    if (!item) throw new Error(`Missing item for id ${id}`);
  }
ready

Revisions

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