Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs for...in vs forEach
(version: 1)
Comparing performance of:
for vs forEach vs for...in
Created:
7 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
arr = []; arr.length = 800000; arr.fill(0)
Tests:
for
let sum = 0; for (let i = 0; i < arr.length; i++) { sum += arr[i]; }
forEach
let sum = 0; arr.forEach(value => sum += value);
for...in
let sum = 0; for (let i in arr) { sum += i; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for
forEach
for...in
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 provided benchmark and its test cases. **Benchmark Definition** The benchmark is designed to compare the performance of three different approaches for iterating over an array in JavaScript: 1. `for` loop 2. `forEach` method (which is a part of the Array prototype) 3. `for...in` loop **Script Preparation Code** The script preparation code initializes an empty array `arr` with a length of 800,000 and fills it with zeros. **Html Preparation Code** There is no HTML preparation code provided. **Test Cases** There are three test cases: 1. **"for"`** * Benchmark Definition: A simple `for` loop that iterates over the array `arr`, adding each element to a running sum. 2. **"forEach"`** * Benchmark Definition: The same as the "for" case, but using the `forEach` method to iterate over the array. 3. **"for...in"`** * Benchmark Definition: A `for...in` loop that iterates over the array's own enumerable properties (i.e., its elements), adding each property value to a running sum. **Options Compared** The benchmark compares the performance of these three approaches in terms of: * Number of executions per second (ExecutionsPerSecond) **Pros and Cons** 1. **`for` loop**: This is a simple, straightforward approach that can be easily understood by many developers. However, it may not be as efficient as other options since it uses a fixed index `i`. 2. **`forEach` method**: This is a part of the Array prototype, which makes it easy to use and understand for developers familiar with JavaScript arrays. It's also relatively efficient, as it avoids the need for manual indexing. 3. **`for...in` loop**: This approach can be confusing for some developers since it iterates over the array's own properties (not its elements). However, it may not be as efficient as `forEach`, as it can involve additional overhead due to property lookup. **Library and Special JS Feature** None of these test cases rely on any specific JavaScript library or special feature. They only use built-in JavaScript functionality. **Other Alternatives** Some alternative approaches that could be tested in a similar benchmark include: * `reduce()` method * Arrow functions (e.g., `(arr, sum) => sum += arr[i]`) * Spread syntax (`for (...i of arr) {...}`) * Other iteration methods (e.g., `while` loop) Keep in mind that the choice of alternatives will depend on the specific goals and requirements of the benchmark. **Device Platform** The latest benchmark results indicate that Chrome 85 running on a Windows Desktop is used for these tests.
Related benchmarks:
Array fill foreach, vs for i loop
for (i < n) vs forEach vs for...of
TypedArray fill vs loop
Array fill map, vs for i loop
Array fill vs for i loop
Comments
Confirm delete:
Do you really want to delete benchmark?