<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var max2 = 10000000; // 10,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 | 1.0 Ops/sec |
Native | 2.0 Ops/sec |
Let's dive into the explanation of what is being tested in this benchmark.
Benchmark Overview
This benchmark compares two approaches to group data by an identifier: Lodash's groupBy
function and JavaScript's built-in Array.prototype.reduce
method.
Options Compared
The two options being compared are:
groupBy
function: This is a utility function from the popular JavaScript library Lodash. It groups an array of objects by a specified key.Array.prototype.reduce
method: This is a standard JavaScript method that applies a reduction function to each element in an array.Pros and Cons
Lodash's groupBy
function:
Pros:
Cons:
JavaScript's built-in Array.prototype.reduce
method:
Pros:
Cons:
groupBy
Other Considerations
When choosing between these two approaches, consider the following factors:
Array.prototype.reduce
method might be a better choice.groupBy
function provides a more readable and concise syntax.Library and Syntax
The library being used in this benchmark is Lodash, which is a popular JavaScript utility library. Its groupBy
function is designed to simplify data grouping and aggregation tasks.
There are no special JavaScript features or syntaxes being tested in this benchmark. The focus is on comparing the performance of two established approaches.
Alternatives
If you're looking for alternatives to these approaches, consider:
Array.prototype.forEach
or Array.prototype.map
, in combination with additional logic to achieve grouping