Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Looptest123
(version: 0)
A test summing 1000 random numbers, 1 - 10000
Comparing performance of:
reduce vs for loop vs forEach vs forOf
Created:
5 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 accum = 0; for(var intCtr=0; intCtr<pArray.length; intCtr++) { accum += pArray[intCtr]; } return accum; } function doForOfLoop(pArray) { var accum = 0; for(const item of pArray) { accum += item; } 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)
forOf
var forEachResult=0 forEachResult = doForOfLoop(arrRandom)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
reduce
for loop
forEach
forOf
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 what is being tested on MeasureThat.net. **Benchmark Definition** The benchmark definition provides a script and a description of the task to be performed. In this case, it creates an array `arrRandom` with 1000 random integers between 1 and 10,000. The script then defines four functions: * `reduceCallback`: takes two arguments, accumulates them, and returns the result. * `doRedeuce`: uses the `reduce` function (a built-in JavaScript method) to apply `reduceCallback` to the array. * `doLoop`: iterates over the array using a traditional for loop, accumulating the values in a variable `accum`. * `doForOfLoop`: uses the `for...of` loop syntax to iterate over the array, accumulating the values in a variable `accum`. * `doForEach`: uses the `forEach` method (a built-in JavaScript method) to iterate over the array, accumulating the values in a variable `accum`. **Options Compared** The benchmark tests four different approaches: 1. `reduce` 2. `for loop` (traditional for loop) 3. `forOf` (for...of loop syntax) 4. `forEach` (forEach method) These options are being compared to determine which one is the fastest. **Pros and Cons of Each Approach** Here's a brief summary: 1. **reduce**: This approach uses the built-in `reduce` method, which is a concise way to perform cumulative operations on arrays. It's likely to be the fastest since it's optimized by the browser. 2. **for loop**: A traditional for loop is straightforward and easy to understand but can be slower than other approaches since it requires manual indexing and iteration. 3. **forOf** loop: The `for...of` loop syntax is a newer way of iterating over arrays, which provides better performance and readability compared to traditional for loops. 4. **forEach**: The `forEach` method is another built-in JavaScript method that allows iterating over arrays. It's similar to `reduce`, but without the cumulative operation. **Library/Functionality** In this case, no external libraries are required or used beyond the standard JavaScript library (ECMAScript). No special JavaScript features or syntax are being tested in this benchmark except for: * ForOf loop syntax * Built-in methods like reduce() and forEach() All other code uses only built-in JavaScript functions. **Alternatives** MeasureThat.net provides a range of alternatives to test different programming scenarios. Some popular alternatives include: * jsBench: A high-performance benchmarking tool for JavaScript. * Benchmark.js: A lightweight, customizable benchmarking library for JavaScript. * Microbenchmarking tools like speedtest.net or benchmark.garden
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?