Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for..of VS forEach
(version: 0)
Comparing performance of:
forEach vs for..of vs forEach func vs for of transpiled
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var values = new Array(10000); for (var i = 0; i < values.length; i++) values[i]=i;
Tests:
forEach
var sum = 0; values.forEach(v => sum += v );
for..of
var sum = 0; for (var v of values) sum += v;
forEach func
var sum = 0; values.forEach(function(v) { sum += v } );
for of transpiled
var sum = 0; for (var _i = 0, _a=values, _l=_a.length; _i < _l; _i++) sum += _a[i];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
forEach
for..of
forEach func
for of transpiled
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
forEach
147094.2 Ops/sec
for..of
164729.0 Ops/sec
forEach func
142929.2 Ops/sec
for of transpiled
46063.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! **Benchmark Definition** The benchmark is designed to compare the performance of three ways to iterate over an array: `forEach`, `for..of`, and a variation of `for..of` with transpilation. In summary, we're comparing: 1. The standard `forEach` method, which takes a callback function as its first argument. 2. The `for..of` loop, which is a newer way to iterate over arrays in JavaScript. 3. A variant of the `for..of` loop that has been transpiled (compiled) to older JavaScript syntax. **Comparison Options** The three options are compared in terms of their execution time, measured in executions per second. * The fastest option will be reported as having the highest "ExecutionsPerSecond" value. * The slower option(s) will have lower "ExecutionsPerSecond" values. **Pros and Cons of Each Approach** 1. **Standard `forEach` method**: * Pros: Simple to implement, widely supported across browsers. * Cons: Can be slower than other approaches due to the overhead of calling a callback function on each iteration. 2. **`for..of` loop**: * Pros: Fast and efficient, allows for better control over iteration variables. * Cons: Requires modern JavaScript support (not compatible with older browsers). 3. **Transpiled `for..of` loop**: * Pros: Allows for compatibility with older JavaScript syntax, potentially reducing compatibility issues. * Cons: May introduce additional overhead due to the transpilation process. **Library and Syntax Used** The `forEach` method uses a built-in JavaScript function, while the `for..of` loops use a new iteration syntax introduced in ECMAScript 2015 (ES6). There are no special JavaScript features or syntaxes used in this benchmark beyond what's already discussed. **Other Alternatives** If you're interested in exploring other ways to iterate over arrays, here are some alternatives: 1. **Array.prototype.reduce()**: This method can be used for array iteration and reduction, but it's not typically used for simple loop iterations. 2. **Array.prototype.map()**: Similar to `reduce()`, but designed specifically for transforming arrays without modifying the original data. Keep in mind that these alternatives may have different performance characteristics or use cases compared to the methods tested in this benchmark. Now that we've explored the world of JavaScript microbenchmarks, it's clear that the choice of iteration method can significantly impact performance. The `for..of` loop appears to be the fastest option, followed closely by the transpiled version. However, compatibility issues may arise when targeting older browsers, making the standard `forEach` method a viable alternative in those cases.
Related benchmarks:
Foreach vs. for vs. while on array looping
forEach vs for i loop addition
for vs foreach vs some vs for..of Kyran
For vs for of vs forEach
typedarry for vs for..of vs forEach
Comments
Confirm delete:
Do you really want to delete benchmark?