Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
flat vs concat test
(version: 3)
Comparing performance of:
concat vs flat
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = Array(1000).fill([0])
Tests:
concat
function flatten(arr) { return Array.prototype.concat.apply([], arr); } flatten(arr)
flat
arr.flat();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
concat
flat
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
concat
63544.5 Ops/sec
flat
67486.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the Benchmark Definition json and explain what is tested on the provided website. **Benchmark Description** The benchmark compares two approaches to flatten an array of arrays in JavaScript: 1. **`Array.prototype.concat.apply([], arr)`**: This approach uses the `concat()` method to concatenate all elements of the input array (`arr`) into a single array. 2. **`arr.flat()`**: This is a modern JavaScript feature introduced in ES6 (ECMAScript 2015) that provides a more concise way to flatten an array of arrays. **Comparison Options** The benchmark compares these two approaches because they have different performance characteristics and use cases: * **`concat()`**: + Pros: widely supported, can be used with older browsers. + Cons: less efficient than `flat()`, requires explicit handling of nested arrays. * **`arr.flat()`**: + Pros: concise, efficient, and easy to read. + Cons: only supported in modern browsers (ES6+) and may not work in older environments. **Library Used** In this benchmark, no library is explicitly used. However, the `concat()` method is a built-in JavaScript method that can be accessed through the `Array.prototype` object. **Special JS Feature or Syntax** The `arr.flat()` syntax uses the modern JavaScript feature called "array flattening" (introduced in ES6). This feature allows you to flatten an array of arrays into a single array using the `flat()` method. **Other Considerations** When choosing between these two approaches, consider the following factors: * **Performance**: If you need high performance and support for older browsers, use `concat()`. For better performance and modern browser support, use `arr.flat()`. * **Readability**: If code readability is crucial, use `arr.flat()` as it is more concise and readable. **Alternatives** If the provided benchmark results are not satisfactory or if you need to compare other approaches, consider using alternative methods: 1. **Using `for...of` loop**: This approach can be used with both modern browsers (ES6+) and older environments. 2. **Using recursion**: A recursive function can be defined to flatten an array of arrays. 3. **Using a third-party library**: Some libraries, like Lodash, provide functions for flattening arrays. Keep in mind that the choice of alternative approach depends on your specific requirements and constraints.
Related benchmarks:
flatMap vs reduce (concat)
flat map vs reduce concat for real
flatMap vs reduce (concatenation)
concat() vs flat()
Comments
Confirm delete:
Do you really want to delete benchmark?