Run details:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36
Chrome 83
Windows
Desktop
4 years ago
Test name Executions per second
one liner 45847.1 Ops/sec
reduced 297678.1 Ops/sec
Tests:
  • one liner

    AخA
     
    const peopleArray = [
      { id: 123, name: "dave", age: 23 },
      { id: 456, name: "chris", age: 23 },
      { id: 789, name: "bob", age: 23 },
      { id: 101, name: "tom", age: 23 },
      { id: 102, name: "tim", age: 23 }
    ]
    const arrayToObject = (arr, keyField) =>
      Object.assign({}, ...arr.map(item => ({[item[keyField]]: item})))
    const peopleObject = arrayToObject(peopleArray, "id")
  • reduced

     
    const peopleArray = [
      { id: 123, name: "dave", age: 23 },
      { id: 456, name: "chris", age: 23 },
      { id: 789, name: "bob", age: 23 },
      { id: 101, name: "tom", age: 23 },
      { id: 102, name: "tim", age: 23 }
    ]
    const arrayToObject = (array, keyField) =>
       array.reduce((obj, item) => {
         obj[item[keyField]] = item
         return obj
       }, {})
    const peopleObject = arrayToObject(peopleArray, "id")