findAllIndexes

Benchmark created on


Setup

const contacts = Array(600).fill(null).map((_, index) => ({ id: index, firstName: 'John', lastName: 'Doe', job: 'Frontend Dev' }));

Test runner

Ready to run.

Testing in
TestOps/sec
for loop
const findAllIndexes = ({ contacts, ids }) => {
  const idSet = new Set(ids);
  const indexes = [];
  let i;

  for (i = 0; indexes.length < ids.length && i < contacts.length; i++) {
    if (contacts[i] && idSet.has(contacts[i].id)) {
      indexes.push(i);
    }
  }

  return indexes;
};

findAllIndexes({ contacts, ids: [1, 2, 3, 4] });
findAllIndexes({ contacts, ids: [100, 200, 300, 400] });
findAllIndexes({ contacts, ids: [596, 597, 598, 599] });
ready
findIndexes
const findAllIndexes = ({ contacts, ids }) => {
  const indexes = [];
  
  for (const id of ids) {
    const index = contacts.findIndex((c) => c.id === id);
    
    if (index > -1) {
      indexes.push(index);
    }
  }
  
  return indexes;
};

findAllIndexes({ contacts, ids: [1, 2, 3, 4] });
findAllIndexes({ contacts, ids: [100, 200, 300, 400] });
findAllIndexes({ contacts, ids: [596, 597, 598, 599] });
ready

Revisions

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