Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
GroupBy vs Reduce
(version: 0)
Comparing performance of:
Lodash vs Reduce
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var data = new Array(1000).fill(null).map(() => Math.random() * 10);
Tests:
Lodash
_.groupBy(data, Math.floor);
Reduce
data.reduce((acc, value) => { const key = Math.floor(value); acc[key] ? acc[key].push(value) : (acc[key] = [ value ]); return acc; }, {})
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash
Reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash
73810.1 Ops/sec
Reduce
9930.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to help you understand the JavaScript microbenchmarking test provided by MeasureThat.net. **Benchmark Overview** The benchmark compares two approaches for grouping data: using Lodash's `groupBy` function and using the built-in `reduce` method of JavaScript arrays. The goal is to determine which approach performs better in terms of execution speed. **Options Compared** Two options are compared: 1. **Lodash's `groupBy` function**: This function takes an array and a callback function as arguments, grouping the elements based on the return value of the callback function. 2. **Built-in `reduce` method**: This method applies a reduction function to each element in an array, accumulating a result. **Pros and Cons of Each Approach** 1. **Lodash's `groupBy` function** * Pros: + More concise and readable code + Built-in library eliminates the need for manual implementation + Potential for better performance due to optimized native code * Cons: + Additional dependency on Lodash library (requires including in project) + May have higher overhead due to function call and object creation 2. **Built-in `reduce` method** * Pros: + No additional dependencies or overhead + More flexible and customizable with custom reduction functions + Native JavaScript implementation, potentially optimized for performance * Cons: + More verbose code required for grouping data + May require more manual error handling and edge cases **Library: Lodash** Lodash is a popular JavaScript library that provides a wide range of utility functions, including `groupBy`. It's designed to make common tasks easier and more efficient. In this benchmark, the `groupBy` function is used to group an array of random numbers based on their floor value. **Special JS Feature/Syntax: None** This benchmark does not use any special JavaScript features or syntax that would be unfamiliar to most software engineers. **Other Alternatives** If you're interested in alternative approaches for grouping data, consider the following: * Using a `Map` object to store and retrieve groups (e.g., `const map = new Map(); ...map.set(key, value);`) * Implementing your own custom grouping function using loops or recursion * Using other libraries like Ramda or Liskov's groupBy functions Keep in mind that the choice of approach depends on the specific requirements of your project and personal preference. **Benchmark Preparation Code** The provided `Script Preparation Code` generates an array of 1000 random numbers, which is used as input for both benchmarking approaches. The `Html Preparation Code` includes a link to the Lodash library, making its functions available for use in the script. **Individual Test Cases** Each test case compares one of the two grouping approaches: 1. **Lodash's `groupBy` function**: This test case uses the `_.groupBy` function from Lodash to group the array of random numbers based on their floor value. 2. **Built-in `reduce` method**: This test case implements a custom reduction function using the built-in `reduce` method of JavaScript arrays. The benchmark results show the execution speed of each approach, with the fastest one being reported as the winner.
Related benchmarks:
lodash groupBy vs Array.reduce (1mln)
lodas2 groupBy vs Array.reduce
lodash groupBy vs Array.reduce on million items
lodash groupBy vs Array.reduce 100k better 2
lodash groupBy vs Array.reduce vs Object.groupBy 100k
Comments
Confirm delete:
Do you really want to delete benchmark?