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")
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((acc, curr) => ({ [curr[keyField]]: curr, acc }), {})
const peopleObject = arrayToObject(peopleArray, "id")
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
one liner | |
reduced | |
reduced clean |
Test name | Executions per second |
---|---|
one liner | 78262.7 Ops/sec |
reduced | 222775.6 Ops/sec |
reduced clean | 50012.7 Ops/sec |
I'll break down the benchmark definition and test cases to explain what's being tested, compared, and its implications.
Benchmark Definition
The benchmark is testing the performance of converting an array into an object using different methods.
In this specific case, there are three variants:
arrayToObject
function that takes two parameters: arr
(the input array) and keyField
(the key to use for object property names).arrayToObject
function:reduced
: uses the reduce
method to iterate over the array.reduced clean
: uses a more concise syntax to achieve the same result.Test Cases
Each test case represents a separate execution of the benchmark, measuring the performance of one of the three variants.
In this example, there are three test cases:
arrayToObject
with two parameters: arr
and keyField
.arrayToObject
function using the reduce
method.arrayToObject
function that achieves the same result in a more concise syntax.Comparison
The main comparison here is between the three variants of the arrayToObject
function:
one liner
and reduced clean
variants are likely to be faster since they use a single expression or a shorter syntax, which can lead to better performance optimization.reduced clean
variant is more concise but may sacrifice readability for the sake of brevity.Pros and Cons
Here are some pros and cons for each approach:
arrayToObject
(original implementation):reduced
:reduced clean
:Library Considerations
There are no libraries explicitly mentioned in the benchmark definition, but the use of the reduce
method implies a familiarity with functional programming concepts.
Special JS Features/Syntax
The test cases do not use any special JavaScript features or syntax. They are written in standard ECMAScript 5+ syntax and rely on the built-in Object.assign
method for creating an empty object.
Alternatives
If you wanted to benchmark alternative approaches, some potential alternatives could include: