Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
GroupBy equiv. without groupBy v2
(version: 1)
Grouping array of objects by key without using groupBy
Comparing performance of:
for-loop vs reduce
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = [] for (let i = 0; i < 5000; i++) { arr.push({key: Math.round(i/100), value: i}) }
Tests:
for-loop
const keys = {} let i let len = arr.length for (let i = 0; i<len; i++) { const it = arr[i] const g = keys[it.key] if (g) { g.items.push(it) } else { keys[it.key] = {items: [], key: it.key} } }
reduce
const keys2 = {} arr.reduce((acc, it) => { const g = acc[it.key] if (g) { g.items.push(it) } else { acc[it.key] = {items: [], key: it.key} } return acc }, keys2)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for-loop
reduce
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 dive into the benchmark and explain what's being tested. **Benchmark Overview** The benchmark is designed to test the performance of two approaches for grouping an array of objects by key: using a `for` loop (approach 1) and using the `reduce()` method (approach 2). The input data consists of an array of objects, each with a unique `key` property and a corresponding `value`. **Approach 1: For-Loop** This approach uses a traditional `for` loop to iterate through the input array. In each iteration, it checks if a group for the current key exists in the `keys` object. If it does, it appends the current item to that group's `items` array. If not, it creates a new group with the same key and initializes its `items` array. **Pros:** * Easy to understand and implement * Suitable for small to medium-sized datasets **Cons:** * Inefficient for large datasets, as it requires multiple iterations through the input data * May lead to performance issues due to the creation of a new object for each unique key **Approach 2: Reduce()** This approach uses the `reduce()` method to iterate through the input array and group its elements by key. The accumulator (`acc`) is an object that stores the groups, which are created and updated as the loop iterates. **Pros:** * Efficient for large datasets, as it only requires a single pass through the input data * Simplifies grouping logic and reduces code duplication **Cons:** * May be less intuitive to understand, especially for those without prior experience with functional programming concepts * Requires proper initialization of the accumulator object **Library: None** The benchmark doesn't use any external libraries or frameworks. It relies solely on standard JavaScript features. **Special JS Features/Syntax: None** There are no special JavaScript features or syntax used in this benchmark, making it accessible to a wide range of developers. **Other Alternatives** If you want to explore alternative approaches, here are some options: * Using `Map` instead of an object for grouping (similar to approach 2) * Utilizing `Array.prototype.forEach()` with a callback function * Leveraging modern JavaScript features like `flatMap()` or `groupBy()` * Considering the use of a different data structure, such as a Trie or a Balanced Binary Search Tree Keep in mind that each alternative may have its own trade-offs and performance characteristics. In conclusion, this benchmark provides a straightforward comparison between two approaches for grouping an array of objects by key. By understanding the pros and cons of each approach, you can choose the most suitable method for your specific use case.
Related benchmarks:
Array range generating
Object keys vs Array map v2
Object.values, For...in, Object.keys Object.entires
Test_123
Map convert
Comments
Confirm delete:
Do you really want to delete benchmark?