Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
flatten arrays 11241243
(version: 0)
Comparing performance of:
Array.flat() vs Reduce vs forEach
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));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.flat()
Reduce
forEach
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 benchmark and explain what's being tested. **Benchmark Definition JSON** The benchmark definition is a script that creates two arrays (`a` and `b`) with 1000 random numbers each, then combines them into an array of arrays (`arrayOfArrays`). This script serves as the input for the various test cases. **Options Compared** Three options are being compared: 1. **Array.flat()**: A method introduced in ECMAScript 2019 (ES10) that flattens an array of arrays into a single array. 2. **Reduce**: An Array.prototype method that reduces an array to a single value by applying a callback function to each element. 3. **forEach**: A method introduced in ECMAScript 2009 (ES5) that executes a provided function once for each element in an array. **Pros and Cons of Each Approach** 1. **Array.flat()**: * Pros: Efficient, concise, and expressive. It's also relatively new, which might make it more likely to be implemented efficiently by browsers. * Cons: Not supported in older browsers (pre-ES10), so it may not be a viable option for testing older JavaScript engines. 2. **Reduce**: * Pros: Widely supported, even in very old browsers (pre-ES5). It's also a built-in method, making it easy to implement. * Cons: Can be less readable and more verbose than other methods, especially for complex data transformations. 3. **forEach**: * Pros: Simple and widely supported. It's also relatively fast, as it avoids creating intermediate arrays. * Cons: May not be the most efficient option, as it requires an explicit loop and may create unnecessary memory allocations. **Library and Purpose** In this benchmark, no external library is being used. However, if a library like Lodash were used, `_.flatten()` or `_.map()` could potentially replace some of these methods. **Special JS Feature or Syntax (None)** There are no special JavaScript features or syntaxes being tested in this benchmark. **Other Alternatives** In addition to the three options being compared, other approaches might include: * Using a library like Lodash for flattening arrays * Using `Array.prototype.map()` to create a new array with flattened values * Using a custom loop and indexing to flatten the array Keep in mind that these alternatives may not be as efficient or readable as the methods being compared.
Related benchmarks:
lodash test
lodash test
flatten arrays 112412432
flatten arrays 1124124323
Comments
Confirm delete:
Do you really want to delete benchmark?