Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.reduce vs for loop vs Array.forEach 1
(version: 0)
A test summing 1000 random numbers, 1 - 10000
Comparing performance of:
reduce vs for loop vs forEach
Created:
4 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<100000; 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 accum = 0; for(var intCtr=0; intCtr<pArray.length; intCtr++) { accum += pArray[intCtr]; } return accum; } function doForEach(pArray) { var accum = 0; pArray.forEach(function(item) { accum += item; }); }
Tests:
reduce
var redeuceResult=0; redeuceResult = doRedeuce(arrRandom);
for loop
var loopResult=0; loopResult = doLoop(arrRandom);
forEach
var forEachResult=0 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 dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Definition** The provided benchmark tests three different approaches to sum up an array of 1000 random integers between 1 and 10,000: 1. **Array.reduce**: Uses the `reduce` method with a custom callback function. 2. **For loop**: Uses a traditional for loop to iterate over the array and accumulate the sum. 3. **Array.forEach**: Uses the `forEach` method to iterate over the array and accumulate the sum. **Options Compared** The benchmark compares the performance of these three approaches: * **Pros and Cons** + **Array.reduce**: - Pros: concise, expressive, and efficient for arrays with a single operation. - Cons: can be slower than traditional loops due to overhead from callback functions. + **For loop**: - Pros: well-known, easy to understand, and suitable for complex logic. - Cons: verbose, less readable, and may not perform as well as other methods. + **Array.forEach**: - Pros: modern, concise, and efficient for arrays with a single operation. - Cons: relatively new method, might not be supported in older browsers or environments. * **Other Considerations** + The benchmark uses a large array of 1000 random integers to ensure consistent results across different browsers and environments. **Library and Its Purpose** The `reduce` method is a built-in JavaScript function that applies a callback function to each element of an array, accumulating a result. In this benchmark, the custom callback function `reduceCallback` adds two numbers together. **Special JS Feature or Syntax** None mentioned in the provided code snippets, but it's worth noting that the `forEach` method was introduced in ECMAScript 2012 (ES6), while the other methods have been part of the language since earlier versions. The benchmark uses modern JavaScript syntax and features, which might affect its compatibility with older browsers or environments. **Other Alternatives** If you're looking for alternative approaches to sum up an array, consider: * **Using `map` followed by `reduce`**: Instead of using `forEach`, you can use the `map` method to create a new array with transformed values and then apply `reduce`. * **Using a library like Lodash**: If you need more flexibility or features, you can use a utility library like Lodash, which provides an `add` function that performs similar logic as the custom callback. In summary, this benchmark helps users compare the performance of three JavaScript methods for summing up an array: `reduce`, `for loop`, and `forEach`. By understanding the pros and cons of each approach, you can choose the most suitable method for your specific use case.
Related benchmarks:
map vs forEach vs for loop
reduce vs for loop
reduce vs for loop
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?