Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Flatten array of array v3
(version: 2)
Comparing performance of:
iterate / push vs reduce / spread vs reduce / concat vs reduce / push vs array.flat vs frray.flatMap
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = []; for (let i = 0; i < 100; i++) { let internal = []; for (let j = 0; j < 100; j++) { internal.push(i * 100 + j); } arr.push(internal); }
Tests:
iterate / push
let flattened = []; for (let i = 0; i < arr.length; i++) { let internal = arr[i]; for (let j = 0; j < internal.length; j++) { flattened.push(internal[j]); } }
reduce / spread
let flattened = arr.reduce((acc, i) => [ ...acc, ...i], []);
reduce / concat
let flattened = arr.reduce((acc, i) => acc.concat(i), []);
reduce / push
let flattened = arr.reduce((acc, i) => { i.forEach(e => acc.push(e)); return acc; }, []);
array.flat
let flattend = arr.flat();
frray.flatMap
let flattend = arr.flatMap((x) => (x));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
iterate / push
reduce / spread
reduce / concat
reduce / push
array.flat
frray.flatMap
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):
I'll break down the benchmark and its test cases to help understand what's being tested. **Benchmark Overview** The `Flatten array of array v3` benchmark measures the performance of different methods for flattening an array of arrays in JavaScript. **Test Cases** There are six test cases: 1. **"iterate / push"`**: This method iterates over each inner array and pushes its elements to a new array using a loop. 2. **"reduce / spread"`**: This method uses the `Array.prototype.reduce()` method with the `spread operator` (`...`) to flatten the array. 3. **"reduce / concat"`**: Similar to the previous one, but uses `Array.prototype.concat()` instead of the spread operator. 4. **"reduce / push"`**: This method uses a custom implementation that iterates over each inner array and pushes its elements to a new array using a loop. 5. **"array.flat"`**: This method uses the `Array.prototype.flat()` method to flatten the array. 6. **"frray.flatMap"`**: This method is not a standard JavaScript method; it seems to be a typo or an experimental feature (more on this later). **Comparison of Methods** The main differences between these methods are: * **Performance**: The `reduce / spread` and `array.flat()` methods tend to be faster due to their use of optimized algorithms. * **Memory usage**: The `iterate / push` method uses more memory because it creates a new array for each inner array, whereas the other methods flatten the array in place or use iterators to minimize memory allocation. * **Code complexity**: Some methods, like `reduce / push`, are more straightforward and easier to understand, while others, like `frray.flatMap`, may be less intuitive due to their experimental nature. **Other Considerations** * **Browser support**: The benchmark results show that Chrome 111 is the fastest browser for most test cases. However, it's essential to note that different browsers may have varying levels of support for JavaScript features and methods. * **Experimental features**: `frray.flatMap` is not a standard JavaScript method and might be considered experimental or non-standard. It's crucial to ensure that any benchmarking or performance testing includes consideration for browser support and compatibility. **Alternatives** If you're interested in exploring alternative methods for flattening arrays, here are a few: * Using `Array.prototype.reduce()` with a custom callback function. * Utilizing libraries like Lodash or Ramda, which provide optimized array manipulation functions. * Employing iterative approaches with loops and push/push/pop operations. Keep in mind that the performance of these alternatives may vary depending on the specific use case and browser support.
Related benchmarks:
Push array 0 index with splice and spread
empty an array in JavaScript?(Yorkie)1
Array spread vs push 2
Test array and unshift
Spread vs Push3
Comments
Confirm delete:
Do you really want to delete benchmark?