<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var max2 = 1000000; // 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 | 14.6 Ops/sec |
Native | 65.7 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Overview
The benchmark is comparing two approaches to group data by an identifier: using Lodash's groupBy
function and using the native JavaScript reduce
method. The benchmark creates a large dataset of objects with an id
property, ranging from 0 to 100 million, and then tests which approach can process this data more efficiently.
Options Compared
Two options are being compared:
groupBy
function: This is a utility function that groups an array of objects by a key function. In this case, the key function is a simple identity function (id
) that returns the id
property of each object.reduce
method: This is a built-in method that applies a reducer function to each element in an array, accumulating a result.Pros and Cons
Here's a brief summary of the pros and cons of each approach:
groupBy
function:reduce
method:Library - Lodash
Lodash is a popular utility library for JavaScript that provides a wide range of functions for tasks like array manipulation, string processing, and object transformation. In this benchmark, Lodash's groupBy
function is used to group the data by the id
property.
Special JS Feature/Syntax - None
There are no special JavaScript features or syntax being tested in this benchmark. The code is straightforward and easy to understand for any software engineer familiar with JavaScript.
Other Alternatives
If you're looking for alternative approaches to group data, here are a few options:
groupby
function: A part of the D3.js library, which is specifically designed for data visualization.groupBy
function: Another utility library that provides a groupBy
function.In summary, the benchmark is comparing two approaches to group data: using Lodash's groupBy
function and using the native JavaScript reduce
method. The results will help determine which approach is faster and more efficient for this specific use case.