Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Perf _.groupBy vs native Array.reduce
(version: 0)
Comparing performance of:
_.groupBy vs Native Array reduce
Created:
4 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/underscore@1.13.1/underscore-umd-min.js"></script>
Tests:
_.groupBy
const a = ["one", "two", "three"]; _.groupBy(a, "length");
Native Array reduce
const a = ["one", "two", "three"]; a.reduce((acc, curr) => { acc[curr.length] = (acc[curr.length] || []); acc[curr.length].push(curr); return acc; }, {});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
_.groupBy
Native Array reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36 Edg/144.0.0.0
Browser/OS:
Chrome 144 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
_.groupBy
5274654.0 Ops/sec
Native Array reduce
9064613.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided JSON benchmark. **Benchmark Overview** The benchmark compares the performance of two approaches: 1. Using the _.groupBy() function from the Underscore.js library to group an array by a specific attribute (in this case, "length"). 2. Using native JavaScript's Array.prototype.reduce() method to achieve the same grouping effect. **Options Compared** The two options being compared are: * Option 1: _.groupBy() with Underscore.js + This approach relies on the library to perform the grouping. * Option 2: Native Array.prototype.reduce() + This approach uses native JavaScript's built-in reduce method to achieve the grouping effect. **Pros and Cons** Here's a brief overview of each option: ### _.groupBy() with Underscore.js Pros: * Easy to implement, as it's a pre-built function from an established library. * Can be more concise and readable code. Cons: * Requires the addition of an external library (Underscore.js), which may add overhead or complexity. * May not be optimized for performance, relying on the library's implementation. ### Native Array.prototype.reduce() Pros: * No additional dependencies or libraries are required. * Can take advantage of native JavaScript optimizations and caching. Cons: * Requires more code and a better understanding of the reduce method to implement correctly. * Might lead to less readable code due to its functional programming nature. **Library: Underscore.js** Underscore.js is a utility-first JavaScript library that provides a collection of high-level functions for functional programming, object manipulation, and array transformation. In this case, _.groupBy() is used to group an array by a specific attribute (in this case, "length"). The _.groupBy() function is likely implemented using a combination of techniques, such as: * Iterating over the input array * Creating a hash table or other data structure to store the grouped results * Returning the resulting data structure **Special JS Feature/Syntax: Native Array.prototype.reduce()** Native Array.prototype.reduce() is a built-in method that applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single output value. In this case, we're using it to group the array by a specific attribute. The syntax for reducing an array with _.groupBy() would typically involve: ```javascript _.groupBy(a, 'length') ``` However, since the raw benchmark result only shows the native JavaScript implementation, we can assume that the underlying logic is similar but implemented differently. **Other Alternatives** If you need to group arrays by a specific attribute in JavaScript, other alternatives could be: * Using the Array.prototype.filter() and Array.prototype.reduce() methods together * Implementing your own custom grouping function using loops and data structures (e.g., objects or arrays) * Utilizing libraries like Lodash.js, which provides similar functionality to Underscore.js Keep in mind that each alternative will have its pros and cons, depending on the specific requirements and constraints of your use case.
Related benchmarks:
lodash groupBy vs Array.reduce 100k - test
lodash groupBy vs Array.reduce 100k corrected
lodash groupBy vs Array.reduce vs Array.group 100k
lodash groupBy vs Array.reduce vs Object.groupBy 100k
lodash groupBy vs Array.reduce vs simple for loop
Comments
Confirm delete:
Do you really want to delete benchmark?