Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Test flatmap reduce
(version: 0)
Comparing performance of:
flatMap vs reduce
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const test = [ { state: 'lorem', }, { items: [ { state: 'ipsum' }, { items: [ { state: 'dolor' }, { items: [ { state: 'sit' } ] } ] } ] }, { items: [ { state: 'foo' } ] }, { state: 'bar' } ]
Tests:
flatMap
const test = [ { state: 'lorem', }, { items: [ { state: 'ipsum' }, { items: [ { state: 'dolor' }, { items: [ { state: 'sit' } ] } ] } ] }, { items: [ { state: 'foo' } ] }, { state: 'bar' } ] const res = (arr) => { return arr.flatMap((x) => { if (x.items?.length) { return res(x.items) } return x }) } console.log(res(test))
reduce
const test = [ { state: 'lorem', }, { items: [ { state: 'ipsum' }, { items: [ { state: 'dolor' }, { items: [ { state: 'sit' } ] } ] } ] }, { items: [ { state: 'foo' } ] }, { state: 'bar' } ] const res = (arr) => { return arr.reduce((acc, x) => { if (x.items?.length) { acc = [...acc, ...res(x.items)] } else { acc.push(x) } return acc }, []) } console.log(res(test))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
flatMap
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 break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark is designed to compare two approaches: `flatMap` and `reduce`. Both methods are used to process an array of objects, but they differ in their handling of nested arrays. **Test Case 1: flatMap** In this test case, we have the following JSON data: ```json { "state": "lorem", "items": [ { "state": "ipsum" }, { "items": [ { "state": "dolor" }, { "items": [ { "state": "sit" } ] } ] } ] } ``` The test code uses the `flatMap` method to process this array: ```javascript const res = (arr) => { return arr.flatMap((x) => { if (x.items?.length) { return res(x.items) } return x }) } ``` **Test Case 2: reduce** In this test case, we have the same JSON data as before. The test code uses the `reduce` method to process this array: ```javascript const res = (arr) => { return arr.reduce((acc, x) => { if (x.items?.length) { acc = [...acc, ...res(x.items)] } else { acc.push(x) } return acc }, []) } ``` **Comparison** The main difference between these two approaches is how they handle nested arrays. `flatMap` returns a new array with the flattened elements, while `reduce` accumulates the results in an array. **Pros and Cons** * `flatMap`: + Pros: Efficient use of memory, as it doesn't create intermediate arrays. + Cons: Can be slower for large datasets due to the overhead of recursion. * `reduce`: + Pros: More flexible, as it can accumulate multiple values in a single array. + Cons: Can lead to increased memory usage if not handled carefully. **Library and Special Features** There is no specific library used in this benchmark. However, the use of template literals (`\r\n`) and backticks (`\``) suggests that the test code is using modern JavaScript features. **Other Considerations** The benchmark measures the execution speed of both `flatMap` and `reduce`. The results are presented in terms of executions per second, which gives an idea of the performance difference between the two methods. **Alternatives** If you're interested in exploring alternative approaches, here are a few options: * `forEach`: This method can be used to process each element individually, but it's not suitable for this specific use case. * `map` and `filter`: These methods can be combined with `reduce` to achieve similar results, but they may lead to increased memory usage due to the creation of intermediate arrays. * Third-party libraries: Depending on your specific requirements, you might consider using a library like Lodash or Ramda, which provide optimized implementations of these methods.
Related benchmarks:
test flatMap vs reduce
Reduce vs flatMap performance
flatMap vs reduce on array dict3
flatMap vs reduce with push testtttteste212312
Flatmap vs reduce with objects
Comments
Confirm delete:
Do you really want to delete benchmark?