Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array creation with Array.reduce vs for loop vs Array.forEach(2)
(version: 0)
A test pushing 1000 random numbers, 1 - 10000
Comparing performance of:
reduce vs for loop vs forEach
Created:
7 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
// Create an array of 1000 random intergers between 1 and 10000 var arrRandom = []; for(var intCtr=0; intCtr<1000; intCtr++) { arrRandom.push(Math.floor(Math.random() * Math.floor(10000))); } function reduceCallback(accum, curr) { return [ ...accum, curr ]; } function doRedeuce(pArray) { return pArray.reduce(reduceCallback, []); } function doLoop(pArray) { var newArray = []; for(var intCtr=0; intCtr<pArray.length; intCtr++) { newArray.push(pArray[intCtr]); } return newArray; } function doForEach(pArray) { var newArray = []; pArray.forEach(function(item) { newArray.push(pArray[item]); }); return newArray }
Tests:
reduce
var redeuceResult = doRedeuce(arrRandom);
for loop
var loopResult = doLoop(arrRandom);
forEach
var forEachResult = doForEach(arrRandom)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
reduce
for loop
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 benchmark and explain what is being tested, compared options, their pros and cons, and other considerations. **Benchmark Overview** The provided JSON represents a JavaScript microbenchmark test case, specifically measuring the performance of three different methods for creating an array: `Array.reduce`, `for loop`, and `Array.forEach`. The test pushes 1000 random numbers between 1 and 10,000 into an array. **Comparison Methods** ### 1. Array.prototype.reduce() The `reduce()` method applies a callback function to each element in the array, accumulating a result. In this case, the callback function simply concatenates the current value to the accumulated array. Pros: * Concise and elegant syntax * Can be used for more complex transformations Cons: * May incur additional overhead due to the callback function invocation * Can lead to stack overflow errors if not implemented carefully ### 2. For Loop The `for` loop iterates over the array elements, pushing each value into a new array. Pros: * Well-known and widely supported syntax * Easy to implement and debug Cons: * Can be slower than other methods due to the overhead of explicit iteration * May lead to memory allocation issues if not managed carefully ### 3. Array.prototype.forEach() The `forEach()` method invokes a callback function for each element in the array, but unlike `reduce()`, it does not accumulate any result. Pros: * Similar syntax to `for` loop, making it easy to implement * Avoids stack overflow risks Cons: * Does not accumulate the results like `reduce()` does * May lead to performance issues due to unnecessary callback invocations **Library Used** The test case uses the built-in `Array.prototype.reduce()`, `Array.prototype.forEach()`, and standard JavaScript functions (e.g., `Math.floor()`) without any external libraries. **Special JS Features/Syntax** None are explicitly mentioned in this benchmark. However, it's worth noting that the use of `let` and `const` variables is implied, although not shown in the provided code snippets. **Other Considerations** * The test case pushes 1000 random numbers into an array, which can lead to memory allocation issues if not managed carefully. * The performance difference between these methods may depend on the specific use case and environment. * The benchmark only measures the execution time of each method, but does not consider factors like memory usage or garbage collection. **Alternatives** Other alternatives for creating arrays could include: * Using `Array.from()` to create an array from an iterable * Utilizing libraries like Lodash's `map` function * Implementing a custom iteration mechanism using iterators and generators Keep in mind that the choice of method depends on the specific requirements and constraints of your project.
Related benchmarks:
Array creation with Array.reduce vs for loop vs Array.forEach
Array.reduce vs for loop vs Array.forEach experiments
Array.reduce vs for loop vs Array.forEach experiments - Fix Foreach as function
Array.reduce vs for loops vs Array.forEach
Comments
Confirm delete:
Do you really want to delete benchmark?