Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array Flatten -> Reduce+Concat | Reduce+Spread | Loop+Push| Loop+Push+Spread | Loop+Spread
(version: 0)
Comparing performance of:
Reduce+Concat vs Reduce+Spread vs Loop+Push vs Loop+Push+Spread vs Loop+Spread
Created:
7 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var params = [ [ 1, 2 ], [ 3, 4 ], [ 5, 6 ], [ 7, 8 ], [ 9, 0 ] ]; var flattenReduceConcat = (inputs) => inputs.reduce((acc, input) => acc.concat(input), []); var flattenReduceSpread = (inputs) => inputs.reduce((acc, input) => [...acc, ...input], []); var flattenLoopPush = (inputs) => { var acc = []; for (var i = 0, L = inputs.length; i < L; i++) { var input = inputs[i]; for (var j = 0, l = input.length; j < l; j++) { acc.push(input[j]); } } return acc; } var flattenLoopPushSpread = (inputs) => { var acc = []; for (var i = 0, l = inputs.length; i < l; i++) { acc.push(...inputs[i]); } return acc; } var flattenLoopSpread = (inputs) => { var acc = []; for (var i = 0, l = inputs.length; i < l; i++) { acc = [...acc, ...inputs[i]]; } return acc; }
Tests:
Reduce+Concat
var result = flattenReduceConcat(params);
Reduce+Spread
var result = flattenReduceSpread(params);
Loop+Push
var result = flattenLoopPush(params);
Loop+Push+Spread
var result = flattenLoopPushSpread(params);
Loop+Spread
var result = flattenLoopSpread(params);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Reduce+Concat
Reduce+Spread
Loop+Push
Loop+Push+Spread
Loop+Spread
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 explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark measures how fast different approaches can flatten an array of arrays into a single one-dimensional array. **Approaches Compared** There are four approaches compared: 1. **Reduce+Concat**: uses `reduce()` with `concat()` 2. **Reduce+Spread**: uses `reduce()` with `spread operator` (`...`) 3. **Loop+Push**: uses a loop to push elements into an accumulator array 4. **Loop+Push+Spread** and **Loop+Spread** are variations of the previous two approaches **Pros and Cons** Here's a brief summary: * **Reduce+Concat**: efficient, but can lead to performance issues due to repeated concatenations. Pros: simple, widely supported. Cons: may be slower than other approaches. * **Reduce+Spread**: more efficient than Reduce+Concat, as it avoids repeated concatenations. However, the spread operator is relatively new and may not be supported in older browsers. Pros: faster, concise. Cons: may require newer browsers or polyfills. * **Loop+Push**: straightforward and easy to understand. However, it can lead to performance issues due to repeated push operations. Pros: simple, easy to implement. Cons: slower than other approaches. * **Loop+Push+Spread** and **Loop+Spread**: variations of the previous two approaches that aim to improve performance by using spread operator instead of push. **Library Used** None explicitly mentioned in the benchmark definition or individual test cases. However, some of these approaches may rely on built-in JavaScript methods or libraries like `reduce()`. **Special JS Feature/Syntax** The use of the spread operator (`...`) in **Reduce+Spread** and **Loop+Push+Spread** is a modern JavaScript feature introduced in ECMAScript 2018 (ES9). While most modern browsers support it, it may not be supported in older browsers or environments. **Alternatives** Other approaches to flatten arrays of arrays could include: * Using `Array.prototype.flat()` with optional arguments (introduced in ES7) * Implementing a custom flattening function using recursive techniques * Utilizing libraries like Lodash or Ramda for array manipulation The benchmark's focus on comparing existing approaches highlights the importance of understanding performance considerations when writing efficient JavaScript code.
Related benchmarks:
Benchmark: flatMap vs reduce vs while 2
flatMap vs reduce test 3
filter + map vs reduce 1234567
flatMap vs reduce 99991234
Spread vs. flatMap vs concat (v2)
Comments
Confirm delete:
Do you really want to delete benchmark?