<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var data = [{
name: 'pi',
value: 3.14
},
{
name: 'hundred',
value: 100
},
{
name: 'truthy',
value: true
},
{
name: 'falsy',
value: false
},
{
name: 'estring',
value: '2.71828'
},
];
var keyBy = (array, fn) => {
return array.reduce((acc, value) => {
acc[fn(value)] = value;
return acc;
}, {});
};
var keyBySpread = (array, fn) => {
return array.reduce((acc, value) => ({
acc,
[fn(value)]: value
}), {});
};
var keyByForEach = (array, fn) => {
var acc = {};
_.forEach(array, (value) => {
acc[fn(value)] = value;
});
return acc;
};
var keyByForLoop = (array, fn) => {
var acc = {};
for (let i = 0; i < array.length; i++) {
acc[fn(array[i])] = array[i];
}
return acc;
};
_.keyBy(data, (v) => v.name);
keyBy(data, (v) => v.name);
keyByForEach(data, (v) => v.name);
keyByForLoop(data, (v) => v.name);
keyBySpread(data, (v) => v.name);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash/keyBy | |
keyBy with native reduce | |
keyBy with forEach | |
keyBy with for loop | |
KeyBy with spread operator |
Test name | Executions per second |
---|---|
lodash/keyBy | 3147518.0 Ops/sec |
keyBy with native reduce | 4158269.8 Ops/sec |
keyBy with forEach | 2327895.5 Ops/sec |
keyBy with for loop | 4388728.0 Ops/sec |
KeyBy with spread operator | 949298.6 Ops/sec |
I'll break down the provided benchmark and explain what's being tested, compared, and its implications.
Benchmark Overview
The test measures the performance of four different approaches to create a key-value map from an array of objects:
lodash/keyBy
(Lodash's implementation)keyBy
with native reducekeyByForEach
(using Lodash's forEach
method)keyByForLoop
(using a traditional for loop)What's Being Tested
Each test case is executed multiple times to measure the execution time per second (ExecutionsPerSecond
). The results are then compared across different approaches.
Options Compared
Here's a brief overview of each approach:
keyBy
function uses a combination of reduce and object assignment to create the key-value map.reduce()
method to iterate over the array, creating the key-value map using spread operator ([...acc, [fn(value), value]]
).forEach
method to iterate over the array, adding each object to the result with its key as the property name.Pros and Cons
Here are some general pros and cons of each approach:
Library and Special JS Features
The lodash/keyBy
implementation uses the Lodash library, which provides a concise way to create key-value maps.
There are no special JavaScript features mentioned in this benchmark that aren't already explained above.
Alternatives
Other alternatives for creating key-value maps include:
Array.prototype.reduce()
without spread operator (acc[fn(value)] = value;
)Object.keys()
, map()
, and reduce()
(not shown in this benchmark)Please note that these alternatives might not be as efficient or readable as the approaches tested in this benchmark.
Overall, this benchmark provides a good comparison of different approaches to creating key-value maps in JavaScript, highlighting the trade-offs between conciseness, readability, and performance.