Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
flatten arrays 1124124323
(version: 0)
Comparing performance of:
Array.flat() vs Reduce vs forEach vs for vs Nested for vs Destructuring + reduce
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var entries = 1000; var a = []; var b = []; var c = []; var d = []; var e = []; var f = []; for (var i = 0; i < entries; i++) { a[i] = Math.random(); b[i] = Math.random(); c[i] = Math.random(); d[i] = Math.random(); e[i] = Math.random(); f[i] = Math.random(); } var arrayOfArrays = [a, b, c, d, e, f];
Tests:
Array.flat()
var result1 = arrayOfArrays.flat();
Reduce
var result2 = arrayOfArrays.reduce((a, subArray) => a.concat(subArray), []);
forEach
var result3 = []; arrayOfArrays.forEach((subArray) => Array.prototype.push.apply(result3, subArray));
for
var result4 = []; for (var i = 0; i < arrayOfArrays.length; i++) { Array.prototype.push.apply(result4, arrayOfArrays[i]); }
Nested for
var result5 = []; for (var i = 0; i < arrayOfArrays.length; i++) { for (var j = 0; j < arrayOfArrays[i].length; j++) { result5.push(arrayOfArrays[i][j]); } }
Destructuring + reduce
var result6 = arrayOfArrays.reduce((a, subArray) => {a.push(...subArray); return a;}, []);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
Array.flat()
Reduce
forEach
for
Nested for
Destructuring + 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 provided JSON data and explain what's being tested. **Benchmark Definition** The benchmark is designed to test the performance of different methods for flattening an array of arrays in JavaScript. The script preparation code generates an array of arrays (`arrayOfArrays`) with 1000 entries each, filled with random numbers. **Test Cases** There are six test cases: 1. **Array.flat()**: Tests the `flat()` method, which returns a new flattened array. 2. **Reduce**: Tests the `reduce()` method, which applies a callback function to each element of the array and returns a single value (in this case, an empty array). 3. **forEach**: Tests the `forEach()` method, which executes a provided function for each element in the array. 4. **for**: Tests a traditional `for` loop to iterate through each sub-array and push elements into a new array. 5. **Nested for**: Tests a nested `for` loop structure to flatten the arrays. 6. **Destructuring + reduce**: Tests a combination of destructuring assignment and `reduce()` methods. **Options Compared** Each test case compares different approaches to flattening the array: * `Array.flat()`: uses the `flat()` method, which is a built-in JavaScript method. * `Reduce`: uses the `reduce()` method with an accumulator variable and concatenation. * `forEach`: uses the `forEach()` method with an iterator function. * `for`: uses a traditional `for` loop with array iteration. * `Nested for`: uses nested `for` loops to iterate through sub-arrays. * `Destructuring + reduce`: uses destructuring assignment followed by `reduce()`. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: 1. **Array.flat()**: Fast, easy to use, and built-in. Pros: high performance, concise code. Cons: not all browsers support it. 2. **Reduce**: Flexible, but can be slower due to object creation. Pros: works with any iterable, flexible callback function. Cons: more verbose, potential performance issues. 3. **forEach**: Easy to use, but may incur additional overhead due to iterator functions. Pros: simple and concise, works with any iterable. Cons: potential performance issues, may require additional setup. 4. **for**: Traditional, straightforward approach. Pros: easy to understand, works with any array. Cons: verbose, can be slower than other methods. 5. **Nested for**: May incur higher overhead due to nested loops. Pros: ensures explicit iteration through sub-arrays. Cons: more complex and potentially slower. **Library/Extensions Used** None of the test cases rely on external libraries or extensions beyond standard JavaScript features. **Special JS Features/Syntax** No special JavaScript features or syntax are used in these test cases. The focus is on comparing the performance of built-in methods and traditional loop constructs. **Alternatives** For flattening arrays, other alternatives could include: * `Array.prototype.concat()`: a more explicit approach using concatenation. * `Array.prototype.map()`: can be used to flatten an array by mapping each element to an array with that element as its only element. * Other libraries or modules, such as Lodash or Ramda, which provide additional utility functions for working with arrays. However, the standard JavaScript methods (`flat()`, `reduce()`, and traditional loops) are still the most widely supported and efficient ways to flatten arrays.
Related benchmarks:
lodash test
lodash test
flatten arrays 11241243
flatten arrays 112412432
Comments
Confirm delete:
Do you really want to delete benchmark?