Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Sum array: for vs forEach vs for..in vs for..of vs reduce
(version: 1)
Compare loop performance
Comparing performance of:
for vs foreach vs for in vs for..of vs reduce
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var array = new Array(100); for (let i = 0; i < 100; i++) { array[i] = i * i; }
Tests:
for
let sum = 0 for (let i = 0; i < array.length; i++) { sum += array[i]; }
foreach
let sum = 0; array.forEach(function(i) { sum += i; });
for in
let sum = 0; for (var i in array) { sum += array[i]; }
for..of
let sum = 0; for (var i of array) { sum += i; }
reduce
array.reduce((a, b) => a + b, 0);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
for
foreach
for in
for..of
reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
Browser/OS:
Chrome 129 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
172097.2 Ops/sec
foreach
6051232.0 Ops/sec
for in
240608.6 Ops/sec
for..of
8784730.0 Ops/sec
reduce
10205020.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided JSON represents a JavaScript microbenchmark test case that compares the performance of different looping constructs: `for`, `forEach`, `for..in`, `for..of`, and `reduce`. The goal is to measure which construct is the fastest for summing up elements in an array. **Looping Constructs Comparison** Here's a brief overview of each looping construct: 1. **`for`**: A traditional looping construct that uses an index variable (`i`) to iterate over the array. * Pros: Simple and widely supported, easy to understand and write. * Cons: Can be verbose for large arrays or complex loops. 2. `**forEach**`: An array method that calls a provided callback function for each element in the array. * Pros: Concise and expressive, does not require explicit index management. * Cons: Can be slower due to the overhead of calling an additional function. 3. **`for..in`**: A looping construct that iterates over the properties of an object (or array) using its own index variable (`i`). * Pros: Simple and flexible, can be used for both objects and arrays. * Cons: Can lead to unexpected behavior if not used carefully, as it iterates over property names rather than values. 4. **`for..of`**: A looping construct that iterates over the values of an iterable (such as an array) using its own index variable (`i`). * Pros: Concise and expressive, designed specifically for arrays, making it a good choice for this test case. * Cons: Limited to arrays only; not suitable for objects or other iterables. 5. **`reduce`**: An array method that reduces the elements of an array to a single value using a callback function. * Pros: Concise and expressive, can be used to simplify complex loops. * Cons: Can be slower due to the overhead of calling an additional function. **Library Usage** The test case uses no external libraries. The `forEach` method is a native JavaScript array method that does not require any additional library imports. **Special JS Feature or Syntax** None of the looping constructs in this test case rely on special JavaScript features or syntax, such as async/await, promises, or modern ECMAScript features like arrow functions (although some versions of `forEach` might be implemented with an arrow function internally). **Benchmark Preparation Code** The preparation code generates a large array of 100 elements and populates it with values using a simple loop. This ensures that the testing environment is consistent across all looping constructs. **Alternative Loops** Other common looping constructs not included in this test case are: * `while` loops * `do...while` loops * Array methods like `map`, `filter`, or `every` * Closures and recursive functions Keep in mind that the performance of these alternatives might vary depending on the specific use case and JavaScript engine. When interpreting the benchmark results, consider factors such as: * Device platform and browser version (the provided data is from a single device and browser, so it's essential to compare multiple setups) * Array size: The current test case uses an array of 100 elements. Increasing or decreasing this value can affect the performance differences between looping constructs. * JavaScript engine: Different engines might optimize loops differently, leading to variations in performance. In conclusion, this benchmark highlights the importance of choosing the right looping construct for your specific use case, as different approaches may offer trade-offs between code simplicity, readability, and performance.
Related benchmarks:
sum calculation: forEach vs for vs forof vs reduce 2
for of vs Array.reduce vs Array.forEach vs for i for summing and array of integers
for vs forEach vs for-of vs for-reverse vs reduce (calculate sum)
2Sum array: for vs forEach vs for..in vs for..of vs reduce
Comments
Confirm delete:
Do you really want to delete benchmark?