Dla Michala ;) (v3)

Revision 3 of this benchmark created on


Setup

const appointment = {
	id: '12345',
	alternatives: [
	  { id: 'cd1f8b9d-079a-4d02-987d-d5d4496f4eb8', start: '2020-04-01' },
	  { id: '2c7748fb-ba67-4120-95a2-b3b3dc8dc9aa', start: '2020-04-02' },
      { id: 'cd1641c8-19d5-410d-826f-c7098a6ee73a', start: '2020-04-03' },
      { id: 'f11409da-eb83-44d3-bbd4-d0d41e374e07', start: '2020-04-04' },
      { id: '6084bc6f-98de-4f7f-a928-6b0dc119b845', start: '2020-04-05' },
      { id: '4d2263a2-eebb-40c8-81a3-1b7a2ca3edbc', start: '2020-04-06' },
      { id: 'b013ba3d-ba73-4f1a-bb23-087bafb11b85', start: '2020-04-07' },
      { id: 'bb328c74-0911-4302-b5e1-ad89c9ffcd61', start: '2020-04-08' },	
      { id: '259d186b-d871-4362-8cf2-13cb24826407', start: '2020-04-09' },	      
      { id: '5ac04eb9-06ac-4b58-8de7-9e1025a1e235', start: '2020-04-10' },	 
    ]
}

const patchAppointment = {
  alternatives: [
    { id: 'bb328c74-0911-4302-b5e1-ad89c9ffcd61', start: '2020-04-08' },
    { id: '259d186b-d871-4362-8cf2-13cb24826407', start: '2020-04-09' },
    { id: '5ac04eb9-06ac-4b58-8de7-9e1025a1e235', start: '2020-04-10' },
    { start: '2020-04-11' },
    { start: '2020-04-12' }
  ]
}

Test runner

Ready to run.

Testing in
TestOps/sec
Michal
const updatedIds = patchAppointment.alternatives.map(({ id }) => id).filter(Boolean);

// updated entries topics list
const toKeep = appointment.alternatives
          .filter(({ id}) => updatedIds.includes(id))
          .reduce((l, alt) => {
            l[alt.id] = alt;
            return l
          }, {});
          
const updatedAlternatives = patchAppointment.alternatives.map(({ id, start }) => {
          let alt;
          if (id && toKeep[id]) {
            alt = toKeep[id];
            alt.start = start;
          } else {
            alt = { start }
          }
          return alt;
        });
ready
Piotr
const updatedAlternatives = patchAppointment.alternatives.map(({ id, start, end }) => {
          if (id) {
            const alt = appointment.alternatives.find(a => a.id === id)
            
            if (!alt) {
              throw new Error('Alternative unknown')
            }
            
            alt.start = start
            
            return alt
          }
          
          return { start }
        });
ready

Revisions

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