Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Dumb benchmark #2
(version: 0)
Comparing performance of:
.flatMap() vs .map() then .flat() vs .concat()
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id='fart'> <div id=''> <div id=''></div> </div> <div id=''></div> <div id=''></div> <div id=''></div> <div id=''></div> </div>
Script Preparation code:
var fart = document.getElementById('fart'); var arrayOfArrays = [{ a: fart.querySelectorAll('div') }, { a: fart.querySelectorAll('div') }];
Tests:
.flatMap()
const result = arrayOfArrays.flatMap(x => [...x.a]);
.map() then .flat()
const result = arrayOfArrays.map(x => [...x.a]).flat();
.concat()
const result = [].concat(...arrayOfArrays.map(x => [...x.a]));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
.flatMap()
.map() then .flat()
.concat()
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):
Measuring JavaScript performance is an essential part of ensuring that your code runs efficiently and effectively. The provided benchmark definition json and test cases allow users to compare the performance of different approaches to flattening arrays. **What is tested?** The main options being compared are: 1. **flatMap()**: A method introduced in ECMAScript 2019 (ES11) that concatenates an array of arrays into a single array, without creating intermediate arrays. 2. **map() then .flat()**: A two-step approach that first maps each inner array to a new array using the `map()` method and then flattens the resulting array using the `flat()` method. 3. **concat()**: The old-fashioned way of concatenating arrays by joining them together using the `concat()` method. **Pros and Cons** 1. **flatMap()**: * Pros: Eliminates the need for intermediate arrays, making it more memory-efficient and faster. * Cons: Not supported in older browsers (before ES11). 2. **map() then .flat()**: * Pros: Wide browser support, easy to understand and implement. * Cons: Creates intermediate arrays, which can be slower and more memory-intensive. 3. **concat()**: * Pros: Simple and straightforward, widely supported. * Cons: Inefficient in terms of performance and memory usage. **Library usage** There is no library used in this benchmark. **Special JS features or syntax** None mentioned. **Considerations** When choosing between these approaches, consider the following: * If you need to support older browsers (before ES11), **map() then .flat()` might be a safer choice. * If you prioritize performance and memory efficiency, **flatMap()` is likely the best option. * If simplicity and wide browser support are more important than performance, **concat()` could be the way to go. **Other alternatives** If none of these approaches suit your needs, consider using other methods for flattening arrays, such as: 1. Using the spread operator (`[...array]`) and repeated concatenation. 2. Using a library like Lodash's `flatten` function (if you're willing to add an external dependency). 3. Implementing a custom loop to iterate over each inner array and append elements to a new array. In summary, **flatMap()` is the most efficient approach for flattening arrays in modern browsers, but its support is limited to newer browsers (before ES11). The **map() then .flat()` approach provides wide browser support but may be slower and less memory-efficient. **concat()` is simple but inefficient, making it suitable only for situations where performance isn't a top priority.
Related benchmarks:
Vanilla JS vs Jquery (parents selector)
Array.from() vs Array.prototype.map.call() on NodeList
array.from.map vs array.from with map
array.from.map vs array.from with map vs spread with map
Comments
Confirm delete:
Do you really want to delete benchmark?