Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Group by - reassign vs immutable
(version: 0)
Comparing performance of:
reassign vs immutable
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = [{ id: "1", type: "a" }, { id: "2", type: "a" }, { id: "3", type: "b" }, { id: "4", type: "b" }, { id: "5", type: "a" }]
Tests:
reassign
function groupBy( arr, key ) { return arr.reduce( (acc, cur) => ({ ...acc, [String(acc[key(cur)])]: [...(acc[key(cur)] || []), cur], }), {}, ); } groupBy(arr, (item) => item.type);
immutable
function groupBy( arr, key ) { return arr.reduce((groups, item) => { (groups[key(item)] ||= []).push(item); return groups; }, {}); } groupBy(arr, (item) => item.type);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
reassign
immutable
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and what pros and cons of each approach are. **Benchmark Definition** The benchmark is comparing two approaches to grouping an array by a specific key: 1. `reassign`: This approach modifies the original array in place, creating new arrays for each group. 2. `immutable`: This approach creates a new object to store the grouped data, without modifying the original array. **Script Preparation Code** The script preparation code defines an array of objects: ```javascript var arr = [ { id: "1", type: "a" }, { id: "2", type: "a" }, { id: "3", type: "b" }, { id: "4", type: "b" }, { id: "5", type: "a" } ]; ``` This array is used to test both grouping approaches. **Html Preparation Code** There is no HTML preparation code provided, so we can assume that the benchmark only runs on the JavaScript side. **Individual Test Cases** The two individual test cases are: 1. `reassign`: This test case uses the `reassign` approach to group the array by the `type` key. 2. `immutable`: This test case uses the `immutable` approach to group the array by the `type` key. **Library and Purpose** There is no library explicitly mentioned in the benchmark definition. However, it appears that the `reduce()` method is being used to implement both grouping approaches. **Special JS Feature or Syntax** Neither of the test cases uses any special JavaScript features or syntax beyond the standard `reduce()` method. **Pros and Cons of Each Approach** Here's a brief summary: 1. `reassign`: * Pros: May be faster since it doesn't create new arrays, only modifies the original array. * Cons: Modifies the original array, which may not be desirable in some cases (e.g., when working with external data). 2. `immutable`: * Pros: Does not modify the original array, preserving its integrity. * Cons: May be slower since it creates new arrays for each group. **Other Alternatives** Other approaches to grouping an array by a specific key might include: 1. Using the `Array.prototype.forEach()` method and creating a new object to store the grouped data. 2. Using a library like Lodash's `groupBy()` function, which implements both `reassign` and `immutable` approaches internally. Overall, this benchmark is designed to compare two common approaches to grouping an array by a specific key in JavaScript. The results can help developers understand the performance implications of each approach and choose the one that best fits their use case.
Related benchmarks:
Immutable Map vs Ordered Map vs List
Spread operator vs Immutable.js performance for common use cases
Immutable.Set Union vs Constructing a new plain JS Set
Immutable.Set Union vs Constructing a new plain JS Set - small
object spread vs immutable-js merge 4.3.0
Comments
Confirm delete:
Do you really want to delete benchmark?