Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
flat() vs reduce.concat()
(version: 0)
Comparing flat() with reduce.concat()
Comparing performance of:
reduce.concat() vs flat()
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
reduce.concat()
var params = [[ 1, 2], ["hello", true, 7]]; var other = params.reduce((acc, val) => acc.concat(val));
flat()
var params = [[1, 2], ["hello", true, 7]]; var other = params.flat();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
reduce.concat()
flat()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Browser/OS:
Chrome 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
reduce.concat()
20021260.0 Ops/sec
flat()
10485461.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark for you. **What is tested?** The benchmark compares two ways to flatten an array with nested elements in JavaScript: 1. `reduce.concat()` 2. `flat()` The test case uses an array of arrays, where each inner array contains a mix of values: numbers (`1` and `2`) and strings (`"hello"`). **Options compared** There are two options being compared: * **`reduce()`**: This method calls the provided callback function for each element in the array, passing the accumulator (the previous result) and the current element as arguments. In this case, it's used with `concat()` to flatten the array. * **`flat()`** (new in ECMAScript 2019): This method returns a new flattened array from the elements of an array. **Pros and Cons** * **`reduce.concat()`** + Pros: Allows for more control over the flattening process, as the callback function can manipulate the accumulator and current element. Can be useful when working with complex data structures. + Cons: May be slower than `flat()` due to the additional function calls and checks. * **`flat()`** + Pros: Faster and more concise, making it a good choice for simple flattening tasks. Reduces the number of iterations required to flatten the array. + Cons: Less control over the flattening process compared to `reduce.concat()`. May not work as expected with non-array values. **Library usage** There is no explicit library used in this benchmark, but it's likely that the JavaScript engine being tested (e.g., Firefox 120) has optimized implementations of these methods for performance. **Special JS feature or syntax** No special features or syntax are mentioned in this benchmark. It focuses solely on comparing two standard JavaScript array methods. **Other alternatives** If you need to flatten an array with nested elements, other options might include: * Using `map()` and then concatenating the resulting arrays * Using a recursive function to flatten the array (although this can be slower and less efficient) * Using a library like Lodash's `flattenDeep()` function Keep in mind that the choice of method depends on the specific requirements of your use case.
Related benchmarks:
flat() vs reduce/concat()
flat map vs reduce concat for real
flatMap vs reduce.concat vs reduce.push
flatMap vs reduce (concatenation)
flatMap vs reduce flattern array
Comments
Confirm delete:
Do you really want to delete benchmark?