<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var max2 = 100000; // 100,000,000 (100 Million)
var data = [];
for (var i = 0; i <= max2; i++) { data.push({ id: i }); }
_.groupBy(data, ({ id }) => id)
data.reduce((acc, item) => {
acc[item.id] = item;
return acc;
}, {})
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash | |
Native |
Test name | Executions per second |
---|---|
Lodash | 138.8 Ops/sec |
Native | 273.5 Ops/sec |
Let's break down what's being tested in this benchmark.
Benchmark Overview
The benchmark compares the performance of two approaches to group an array of objects by a common key: lodash groupBy
and Array.reduce
.
Options Compared
_groupBy
function from the Lodash library, which is a utility library for functional programming in JavaScript.Array.reduce
method to achieve the same grouping functionality.Pros and Cons of Each Approach
_groupBy
):Array.reduce
):Library: Lodash
Lodash is a popular utility library for JavaScript that provides a wide range of functional programming helpers. In this case, _groupBy
is used to group arrays by a common key. Lodash's groupBy
function takes two arguments: the array to be grouped and a function that extracts the grouping key from each element.
Special JS Feature/Syntax
None mentioned in this benchmark.
Other Alternatives
If you prefer not to use an external library like Lodash, you could also consider using other libraries or polyfills that provide similar functionality. Some alternatives include:
groupBy
function.groupBy
method for grouping arrays by a common key.For the native approach using Array.reduce
, you could also consider other alternatives like:
groupBy
function for grouping arrays by a common key.groupBy
function for grouping arrays by a common key.Keep in mind that these alternatives may introduce additional overhead or require more manual effort to achieve the desired functionality.