Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs forEach vs for in vs for of (2)
(version: 0)
for vs forEach vs for in vs for of
Comparing performance of:
forEach vs for in vs for vs for of
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; function doMath(a) { return Math.log(a); } for(i=0; i<10000; i++){ arr[i] = i + 1; }
Tests:
forEach
let sum = 0; arr.forEach((value) => { sum += doMath(value); });
for in
let sum = 0; for(const index in arr) { sum += doMath(arr[index]); }
for
let sum = 0; for(var i = 0; i < arr.length; i++) { const value = arr[i]; sum += doMath(value); }
for of
let sum = 0; for(const value of arr) { sum += doMath(value); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
forEach
for in
for
for of
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36
Browser/OS:
Chrome 125 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
forEach
484.2 Ops/sec
for in
382.0 Ops/sec
for
339.1 Ops/sec
for of
535.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll provide an in-depth explanation of the benchmark, its test cases, and options being compared. **Benchmark Overview** The provided JSON represents a JavaScript microbenchmark named "for vs forEach vs for in vs for of (2)". The purpose of this benchmark is to compare the performance of four different iteration mechanisms: `for`, `forEach`, `for...in`, and `for...of`. These iteration mechanisms are commonly used in JavaScript to iterate over arrays or other iterable objects. **Options Being Compared** The options being compared are: 1. **`for` loop**: This is a traditional loop that uses an index variable (`i`) to access elements of the array. 2. **`forEach` method**: This is a built-in method that iterates over an array using a callback function. 3. **`for...in` loop**: This is a loop that iterates over the properties (including indices) of an object, including arrays. 4. **`for...of` loop**: This is a loop that iterates over the values of an iterable object, such as an array. **Pros and Cons** Each iteration mechanism has its pros and cons: 1. **`for` loop**: * Pros: More control over indexing and looping behavior. * Cons: Can be slower due to overhead of manual indexing and looping logic. 2. **`forEach` method**: * Pros: Concise and readable code, no need to worry about indexing. * Cons: May incur additional overhead due to function invocation. 3. **`for...in` loop**: * Pros: Allows iteration over object properties, including indices. * Cons: Can be slower due to unnecessary iterations over undefined properties. 4. **`for...of` loop**: * Pros: Concise and readable code, no need to worry about indexing. * Cons: May not work as expected for arrays with non-numeric indices. **Library Usage** None of the test cases explicitly uses a library, but `forEach` method relies on the built-in `Array.prototype.forEach` function. However, since this is a microbenchmark, the focus is on the iteration mechanisms rather than external libraries. **Special JavaScript Features or Syntax** No special JavaScript features or syntax are used in these benchmark test cases. The examples are straightforward and do not include any advanced topics like async/await, closures, or modern ES6+ features. **Other Alternatives** In addition to the four options being compared, other iteration mechanisms exist in JavaScript, such as: 1. **`map` method**: This creates a new array with the results of applying a provided function to each element. 2. **`reduce` method**: This applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value. These alternatives are not included in the benchmark, as they focus specifically on loop-based iteration mechanisms. I hope this explanation helps you understand the JavaScript microbenchmark!
Related benchmarks:
for vs forEach vs for in vs for of
for vs forEach vs for in vs for of vs lodash
for vs forEach vs for in vs for of vs lodash vs for reverse (2)
for vs forEach vs for in vs for of vs lodash vs for reverse (3)
Comments
Confirm delete:
Do you really want to delete benchmark?