Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
groupby comparisons
(version: 0)
Comparing performance of:
Vanilla Reduce vs Native groupBy vs ramda vs lodash
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.29.1/ramda.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
Script Preparation code:
var dag = { nodes: [{ id: '1' }, { id: '2' }, { id: '3' }, { id: '4' }], links: [{ fromId: '1', fromPort: 'Output', toId: '2', toPort: 'Input' }, { fromId: '1', fromPort: 'Output', toId: '3', toPort: 'Input' }, { fromId: '2', fromPort: 'Output', toId: '4', toPort: 'Input' }, { fromId: '3', fromPort: 'Output', toId: '4', toPort: 'Input' }, ] }
Tests:
Vanilla Reduce
var incomingLinksById = dag.links.reduce((acc, link) => { if (!acc[link.toId]) { acc[link.toId] = [link]; } else { acc[link.toId].push(link); } return acc; }, {});
Native groupBy
var incomingLinksById = Object.groupBy(dag.links, l => l.toId);
ramda
var incomingLinksById = R.groupBy(l => l.toId, dag.links)
lodash
var incomingLinksById = _.groupBy(dag.links, l => l.toId)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Vanilla Reduce
Native groupBy
ramda
lodash
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Browser/OS:
Chrome 123 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Vanilla Reduce
3105965.8 Ops/sec
Native groupBy
1416976.6 Ops/sec
ramda
831218.4 Ops/sec
lodash
1954673.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Definition** The benchmark is designed to measure the performance of different approaches to group by an array of objects, specifically in this case, grouping by `toId` in the `dag.links` array. The goal is to find the most efficient way to achieve this grouping operation. **Options Compared** There are four options compared: 1. **Vanilla Reduce**: This option uses the built-in `reduce()` method on the Array prototype to group the objects. It iterates over the array, accumulating an object that contains all the elements with a specific key (in this case, `toId`). 2. **Native groupBy**: This option uses the native `groupBy()` function in JavaScript, which is supported by most modern browsers. It's designed for simplicity and performance. 3. **ramda**: This option uses the Ramda library, a popular functional programming library for JavaScript. Specifically, it uses the `groupBy` function from Ramda to achieve grouping. 4. **lodash**: This option uses the Lodash library, another popular utility library for JavaScript. It provides a `groupBy` function that can be used for grouping. **Pros and Cons of Each Approach** Here's a brief overview of each approach: * **Vanilla Reduce**: This approach is concise and easy to understand but can be less efficient than native functions or specialized libraries like Ramda or Lodash, especially for large datasets. It's also limited by the browser support, as `reduce()` is not supported in older browsers. * **Native groupBy**: This approach is widely supported and efficient but might not be immediately apparent to developers without experience with JavaScript built-ins. * **ramda**: This approach provides a flexible and composable way to perform grouping operations, making it suitable for complex data processing tasks. However, using an external library can introduce additional overhead due to the need to load and initialize the library. * **lodash**: Similar to Ramda, this approach offers a robust set of utility functions, including `groupBy`. It's widely supported but requires loading the Lodash library, which might add unnecessary overhead for small datasets. **Other Considerations** When evaluating these approaches, consider factors such as: * **Browser support**: Vanilla Reduce is limited by browser support, while native functions like groupBy are generally well-supported. * **Performance**: Native functions and specialized libraries like Ramda or Lodash tend to be faster than vanilla implementations due to their optimized implementation and caching. * **Complexity**: ramda and lodash offer more complex but flexible ways to perform grouping operations, making them suitable for data processing tasks that require multiple transformations. **Library Used** In this benchmark, the `groupBy` function is used from: * Lodash: `_.groupBy(dag.links, l => l.toId)` * Ramda: `R.groupBy(l => l.toId, dag.links)` The native `groupBy()` function in JavaScript is not shown as a separate option since it's built-in and widely supported. **Special JS Features or Syntax** None of the benchmark options use any special JavaScript features or syntax.
Related benchmarks:
lodash groupBy vs Array.reduce vs Ramda on million items
lodash groupBy vs lodash keyBy 2
groupBy vs partition
lodash groupBy vs filter 100k v1
lodash keyBy and groupBy
Comments
Confirm delete:
Do you really want to delete benchmark?