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")
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")
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
one liner | |
reduced |
Test name | Executions per second |
---|---|
one liner | 209709.9 Ops/sec |
reduced | 882758.6 Ops/sec |
Let's break down the provided benchmark and its test cases.
Benchmark Description
The benchmark is designed to measure the performance of converting an array into an object, specifically using the Object.assign
or reduce
method.
Script Preparation Code
There is no preparation code provided for either test case. This means that the benchmark starts with a clean slate, and the execution time is solely dependent on the conversion process itself.
Individual Test Cases
The benchmark consists of two test cases:
** This test case uses the
Object.assignmethod to convert the array into an object. The code snippet demonstrates how to use
map,
reduce, and
Object.assign` together to achieve this conversion.Pros of using Object.assign
:
Cons of using Object.assign
:
** This test case uses the
reducemethod to convert the array into an object. The code snippet demonstrates how to use the
reduce` method with a callback function to achieve this conversion.Pros of using reduce
:
Object.assign
for large datasetsCons of using reduce
:
Object.assign
Library Usage
There is no explicit library usage mentioned in either test case. However, the Object
and Array
prototypes are used implicitly.
Special JS Features/Syntax
None of the test cases use any special JavaScript features or syntax beyond what is considered standard at this point (ES6+).
Alternatives
If you're looking for alternative approaches to converting arrays into objects, here are a few options:
Object.assign
or reduce
. It's more efficient than Object.assign
but may require more complex code.map()
method to create an array of key-value pairs, which is then passed to Object.fromEntries()
. This method can be more concise and readable than using reduce
.objectAssign()
function: If you're already relying on Lodash for other utility functions, you can use its objectAssign
function to convert arrays into objects.In conclusion, the choice of approach depends on your specific requirements, such as performance, conciseness, and code readability. The benchmark results provided by MeasureThat.net will help you determine which approach is most suitable for your use case.