Crew Merge (v4)

Revision 4 of this benchmark created on


Test runner

Ready to run.

Testing in
TestOps/sec
Reduce
const data = [
  {
    "adult": false,
    "gender": 2,
    "id": 137427,
    "known_for_department": "Directing",
    "name": "Denis Villeneuve",
    "original_name": "Denis Villeneuve",
    "popularity": 35.625,
    "profile_path": "/433lXlkdMGXzrpwnKM4Ul1sln15.jpg",
    "credit_id": "5e959beedb72c00012ad7dcf",
    "department": "Directing",
    "job": "Director"
  },
  {
    "adult": false,
    "gender": 2,
    "id": 564940,
    "known_for_department": "Writing",
    "name": "Jon Spaihts",
    "original_name": "Jon Spaihts",
    "popularity": 7.444,
    "profile_path": "/jzIwgRpEG2VXYHmIyk0vpb6S3TA.jpg",
    "credit_id": "5e9ca1bfe22d28001b61990e",
    "department": "Writing",
    "job": "Screenplay"
  },
  {
    "adult": false,
    "gender": 2,
    "id": 137427,
    "known_for_department": "Directing",
    "name": "Denis Villeneuve",
    "original_name": "Denis Villeneuve",
    "popularity": 35.625,
    "profile_path": "/433lXlkdMGXzrpwnKM4Ul1sln15.jpg",
    "credit_id": "6021f06b458199003da81ad8",
    "department": "Production",
    "job": "Producer"
  },
  {
    "adult": false,
    "gender": 2,
    "id": 564940,
    "known_for_department": "Writing",
    "name": "Jon Spaihts",
    "original_name": "Jon Spaihts",
    "popularity": 7.444,
    "profile_path": "/jzIwgRpEG2VXYHmIyk0vpb6S3TA.jpg",
    "credit_id": "614c0aa42dffd800668b0ce4",
    "department": "Production",
    "job": "Executive Producer"
  },
  {
    "adult": false,
    "gender": 2,
    "id": 137427,
    "known_for_department": "Directing",
    "name": "Denis Villeneuve",
    "original_name": "Denis Villeneuve",
    "popularity": 35.625,
    "profile_path": "/433lXlkdMGXzrpwnKM4Ul1sln15.jpg",
    "credit_id": "6178ed77c2b9df0062239903",
    "department": "Writing",
    "job": "Screenplay"
  },
  {
    "adult": false,
    "gender": 2,
    "id": 1315700,
    "known_for_department": "Costume & Make-Up",
    "name": "Donald Mowat",
    "original_name": "Donald Mowat",
    "popularity": 2.129,
    "profile_path": "/ikdaVXZvwu9wdgbtJWbeokb0fVL.jpg",
    "credit_id": "62d5b37f04b596005413b770",
    "department": "Costume & Make-Up",
    "job": "Prosthetic Designer"
  },
  {
    "adult": false,
    "gender": 2,
    "id": 1315700,
    "known_for_department": "Costume & Make-Up",
    "name": "Donald Mowat",
    "original_name": "Donald Mowat",
    "popularity": 2.129,
    "profile_path": "/ikdaVXZvwu9wdgbtJWbeokb0fVL.jpg",
    "credit_id": "62d5b3f9c92c5d0054e78816",
    "department": "Costume & Make-Up",
    "job": "Hair Designer"
  },
  {
    "adult": false,
    "gender": 0,
    "id": 1426313,
    "known_for_department": "Production",
    "name": "Jessica Derhammer",
    "original_name": "Jessica Derhammer",
    "popularity": 3.626,
    "profile_path": null,
    "credit_id": "62eaad366d9fe8005eedab2a",
    "department": "Production",
    "job": "Co-Producer"
  },
  {
    "adult": false,
    "gender": 0,
    "id": 1426313,
    "known_for_department": "Production",
    "name": "Jessica Derhammer",
    "original_name": "Jessica Derhammer",
    "popularity": 3.626,
    "profile_path": null,
    "credit_id": "62eaad4748333a0061eb27c6",
    "department": "Production",
    "job": "Unit Production Manager"
  },
  {
    "adult": false,
    "gender": 2,
    "id": 1315700,
    "known_for_department": "Costume & Make-Up",
    "name": "Donald Mowat",
    "original_name": "Donald Mowat",
    "popularity": 2.129,
    "profile_path": "/ikdaVXZvwu9wdgbtJWbeokb0fVL.jpg",
    "credit_id": "65d12da9bf31f2016360b3e1",
    "department": "Costume & Make-Up",
    "job": "Makeup Department Head"
  }
]
const mergedData = data.reduce((acc, obj, index) => {
  const existingIndex = acc.findIndex(item => item.name === obj.name);
  if (existingIndex !== -1) {
    acc[existingIndex].job.push(obj.job);
  } else {
    const newObj = {...obj};
    newObj.job = [newObj.job];
    acc.push(newObj);
  }
  return acc;
}, []);
ready
Reduce
const data = [
  {
    "adult": false,
    "gender": 2,
    "id": 137427,
    "known_for_department": "Directing",
    "name": "Denis Villeneuve",
    "original_name": "Denis Villeneuve",
    "popularity": 35.625,
    "profile_path": "/433lXlkdMGXzrpwnKM4Ul1sln15.jpg",
    "credit_id": "5e959beedb72c00012ad7dcf",
    "department": "Directing",
    "job": "Director"
  },
  {
    "adult": false,
    "gender": 2,
    "id": 564940,
    "known_for_department": "Writing",
    "name": "Jon Spaihts",
    "original_name": "Jon Spaihts",
    "popularity": 7.444,
    "profile_path": "/jzIwgRpEG2VXYHmIyk0vpb6S3TA.jpg",
    "credit_id": "5e9ca1bfe22d28001b61990e",
    "department": "Writing",
    "job": "Screenplay"
  },
  {
    "adult": false,
    "gender": 2,
    "id": 137427,
    "known_for_department": "Directing",
    "name": "Denis Villeneuve",
    "original_name": "Denis Villeneuve",
    "popularity": 35.625,
    "profile_path": "/433lXlkdMGXzrpwnKM4Ul1sln15.jpg",
    "credit_id": "6021f06b458199003da81ad8",
    "department": "Production",
    "job": "Producer"
  },
  {
    "adult": false,
    "gender": 2,
    "id": 564940,
    "known_for_department": "Writing",
    "name": "Jon Spaihts",
    "original_name": "Jon Spaihts",
    "popularity": 7.444,
    "profile_path": "/jzIwgRpEG2VXYHmIyk0vpb6S3TA.jpg",
    "credit_id": "614c0aa42dffd800668b0ce4",
    "department": "Production",
    "job": "Executive Producer"
  },
  {
    "adult": false,
    "gender": 2,
    "id": 137427,
    "known_for_department": "Directing",
    "name": "Denis Villeneuve",
    "original_name": "Denis Villeneuve",
    "popularity": 35.625,
    "profile_path": "/433lXlkdMGXzrpwnKM4Ul1sln15.jpg",
    "credit_id": "6178ed77c2b9df0062239903",
    "department": "Writing",
    "job": "Screenplay"
  },
  {
    "adult": false,
    "gender": 2,
    "id": 1315700,
    "known_for_department": "Costume & Make-Up",
    "name": "Donald Mowat",
    "original_name": "Donald Mowat",
    "popularity": 2.129,
    "profile_path": "/ikdaVXZvwu9wdgbtJWbeokb0fVL.jpg",
    "credit_id": "62d5b37f04b596005413b770",
    "department": "Costume & Make-Up",
    "job": "Prosthetic Designer"
  },
  {
    "adult": false,
    "gender": 2,
    "id": 1315700,
    "known_for_department": "Costume & Make-Up",
    "name": "Donald Mowat",
    "original_name": "Donald Mowat",
    "popularity": 2.129,
    "profile_path": "/ikdaVXZvwu9wdgbtJWbeokb0fVL.jpg",
    "credit_id": "62d5b3f9c92c5d0054e78816",
    "department": "Costume & Make-Up",
    "job": "Hair Designer"
  },
  {
    "adult": false,
    "gender": 0,
    "id": 1426313,
    "known_for_department": "Production",
    "name": "Jessica Derhammer",
    "original_name": "Jessica Derhammer",
    "popularity": 3.626,
    "profile_path": null,
    "credit_id": "62eaad366d9fe8005eedab2a",
    "department": "Production",
    "job": "Co-Producer"
  },
  {
    "adult": false,
    "gender": 0,
    "id": 1426313,
    "known_for_department": "Production",
    "name": "Jessica Derhammer",
    "original_name": "Jessica Derhammer",
    "popularity": 3.626,
    "profile_path": null,
    "credit_id": "62eaad4748333a0061eb27c6",
    "department": "Production",
    "job": "Unit Production Manager"
  },
  {
    "adult": false,
    "gender": 2,
    "id": 1315700,
    "known_for_department": "Costume & Make-Up",
    "name": "Donald Mowat",
    "original_name": "Donald Mowat",
    "popularity": 2.129,
    "profile_path": "/ikdaVXZvwu9wdgbtJWbeokb0fVL.jpg",
    "credit_id": "65d12da9bf31f2016360b3e1",
    "department": "Costume & Make-Up",
    "job": "Makeup Department Head"
  }
]

const mergedData = data.reduce((acc, obj) => {
  const existingObjIndex = acc.findIndex(item => item.name === obj.name);
  if (existingObjIndex !== -1) {
    acc[existingObjIndex].job.push(obj.job);
  } else {
    obj.job = [obj.job];
    acc.push(obj);
  }
  return acc;
}, []);
ready
Test 3
const data = [
  {
    "adult": false,
    "gender": 2,
    "id": 137427,
    "known_for_department": "Directing",
    "name": "Denis Villeneuve",
    "original_name": "Denis Villeneuve",
    "popularity": 35.625,
    "profile_path": "/433lXlkdMGXzrpwnKM4Ul1sln15.jpg",
    "credit_id": "5e959beedb72c00012ad7dcf",
    "department": "Directing",
    "job": "Director"
  },
  {
    "adult": false,
    "gender": 2,
    "id": 564940,
    "known_for_department": "Writing",
    "name": "Jon Spaihts",
    "original_name": "Jon Spaihts",
    "popularity": 7.444,
    "profile_path": "/jzIwgRpEG2VXYHmIyk0vpb6S3TA.jpg",
    "credit_id": "5e9ca1bfe22d28001b61990e",
    "department": "Writing",
    "job": "Screenplay"
  },
  {
    "adult": false,
    "gender": 2,
    "id": 137427,
    "known_for_department": "Directing",
    "name": "Denis Villeneuve",
    "original_name": "Denis Villeneuve",
    "popularity": 35.625,
    "profile_path": "/433lXlkdMGXzrpwnKM4Ul1sln15.jpg",
    "credit_id": "6021f06b458199003da81ad8",
    "department": "Production",
    "job": "Producer"
  },
  {
    "adult": false,
    "gender": 2,
    "id": 564940,
    "known_for_department": "Writing",
    "name": "Jon Spaihts",
    "original_name": "Jon Spaihts",
    "popularity": 7.444,
    "profile_path": "/jzIwgRpEG2VXYHmIyk0vpb6S3TA.jpg",
    "credit_id": "614c0aa42dffd800668b0ce4",
    "department": "Production",
    "job": "Executive Producer"
  },
  {
    "adult": false,
    "gender": 2,
    "id": 137427,
    "known_for_department": "Directing",
    "name": "Denis Villeneuve",
    "original_name": "Denis Villeneuve",
    "popularity": 35.625,
    "profile_path": "/433lXlkdMGXzrpwnKM4Ul1sln15.jpg",
    "credit_id": "6178ed77c2b9df0062239903",
    "department": "Writing",
    "job": "Screenplay"
  },
  {
    "adult": false,
    "gender": 2,
    "id": 1315700,
    "known_for_department": "Costume & Make-Up",
    "name": "Donald Mowat",
    "original_name": "Donald Mowat",
    "popularity": 2.129,
    "profile_path": "/ikdaVXZvwu9wdgbtJWbeokb0fVL.jpg",
    "credit_id": "62d5b37f04b596005413b770",
    "department": "Costume & Make-Up",
    "job": "Prosthetic Designer"
  },
  {
    "adult": false,
    "gender": 2,
    "id": 1315700,
    "known_for_department": "Costume & Make-Up",
    "name": "Donald Mowat",
    "original_name": "Donald Mowat",
    "popularity": 2.129,
    "profile_path": "/ikdaVXZvwu9wdgbtJWbeokb0fVL.jpg",
    "credit_id": "62d5b3f9c92c5d0054e78816",
    "department": "Costume & Make-Up",
    "job": "Hair Designer"
  },
  {
    "adult": false,
    "gender": 0,
    "id": 1426313,
    "known_for_department": "Production",
    "name": "Jessica Derhammer",
    "original_name": "Jessica Derhammer",
    "popularity": 3.626,
    "profile_path": null,
    "credit_id": "62eaad366d9fe8005eedab2a",
    "department": "Production",
    "job": "Co-Producer"
  },
  {
    "adult": false,
    "gender": 0,
    "id": 1426313,
    "known_for_department": "Production",
    "name": "Jessica Derhammer",
    "original_name": "Jessica Derhammer",
    "popularity": 3.626,
    "profile_path": null,
    "credit_id": "62eaad4748333a0061eb27c6",
    "department": "Production",
    "job": "Unit Production Manager"
  },
  {
    "adult": false,
    "gender": 2,
    "id": 1315700,
    "known_for_department": "Costume & Make-Up",
    "name": "Donald Mowat",
    "original_name": "Donald Mowat",
    "popularity": 2.129,
    "profile_path": "/ikdaVXZvwu9wdgbtJWbeokb0fVL.jpg",
    "credit_id": "65d12da9bf31f2016360b3e1",
    "department": "Costume & Make-Up",
    "job": "Makeup Department Head"
  }
]

const mergedData = data.reduce((acc, obj) => {
    const existingObj = acc.find((item) => item.id === obj.id);

    const addCreditToCredits = () => {
      return [
        ...existingObj.credits,
        { credit_id: obj.credit_id, department: obj.department, job: obj.job },
      ];
    };

    const addInitialCreditsToCrew = () => {
      return [
        ...acc,
        {
          ...obj,
          credits: [
            {
              credit_id: obj.credit_id,
              department: obj.department,
              job: obj.job,
            },
          ],
        },
      ];
    };

    if (existingObj) {
      existingObj.credits = addCreditToCredits();
    } else {
      acc = addInitialCreditsToCrew();
    }

    return acc;
  }, []);
ready

Revisions

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