UpdateContacts

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
map



const updateContact = ({ contacts, id, updates }) =>
  contacts.map((contact) => (contact.id === id ? { ...contact, ...updates } : contact));


updateContact({ contacts, id: 1, updates: { job: 'Backend Dev' } });
updateContact({ contacts, id: 100, updates: { job: 'Backend Dev' } });
updateContact({ contacts, id: 200, updates: { job: 'Backend Dev' } });
updateContact({ contacts, id: 300, updates: { job: 'Backend Dev' } });
updateContact({ contacts, id: 400, updates: { job: 'Backend Dev' } });
updateContact({ contacts, id: 500, updates: { job: 'Backend Dev' } });
updateContact({ contacts, id: 599, updates: { job: 'Backend Dev' } });
ready
Indexes


const updateContact = ({ contacts, id, updates }) => {
  const index = contacts.findIndex((contact) => contact.id === id);

  if (index === -1) {
    return contacts;
  }

  const updatedContacts = [...contacts];

  if (!contacts[index]) {
    return contacts;
  }

  updatedContacts[index] = { ...contacts[index], ...updates };

  return updatedContacts;
};

updateContact({ contacts, id: 1, updates: { job: 'Backend Dev' } });
updateContact({ contacts, id: 100, updates: { job: 'Backend Dev' } });
updateContact({ contacts, id: 200, updates: { job: 'Backend Dev' } });
updateContact({ contacts, id: 300, updates: { job: 'Backend Dev' } });
updateContact({ contacts, id: 400, updates: { job: 'Backend Dev' } });
updateContact({ contacts, id: 500, updates: { job: 'Backend Dev' } });
updateContact({ contacts, id: 599, updates: { job: 'Backend Dev' } });
ready

Revisions

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