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 vs for of transpiled simple
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; function add(v) { sum += v} values.forEach(add);
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, values_1 = values; _i < values_1.length; _i++) { var v = values_1[_i]; sum += v; }
for of transpiled simple
var sum = 0; for (var i = 0; i < values.length; i++) sum += values[i];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
forEach
for..of
forEach func
for of transpiled
for of transpiled simple
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 dive into the world of JavaScript microbenchmarks. **What is being tested?** The provided JSON represents a benchmark test that compares the performance of three different approaches to iterate over an array in modern JavaScript: 1. **for..of**: A built-in iteration syntax introduced in ECMAScript 2015. 2. **forEach**: A method on Array objects, also part of ECMAScript 2015. 3. Two variants of `for` loops: * **For loop with function callback**: A classic `for` loop with a separate function passed as an argument to iterate over the array elements. * **Transpiled for loop**: A `for` loop that uses the transpiled version of the `for...of` syntax, which is compatible with older JavaScript engines. **Options compared:** The benchmark compares the performance of each approach: 1. **For..of** 2. **forEach func** (a function callback variant of `forEach`) 3. **For loop with function callback** 4. **Transpiled for loop** (a transpiled version of a classic `for` loop) **Pros and Cons:** Here's a brief summary of the pros and cons of each approach: 1. **For..of**: * Pros: Native support, concise syntax, easy to read. * Cons: May not be supported in older browsers or engines. 2. **forEach func** (function callback variant): * Pros: Works with any array-like object, can be used with other data structures. * Cons: Requires an additional function definition, less efficient due to the overhead of function creation. 3. **For loop with function callback**: * Pros: Wide browser support, easy to implement. * Cons: Less concise syntax, more prone to errors due to variable scoping. 4. **Transpiled for loop** (transpiled version of classic `for` loop): * Pros: Compatibility with older browsers and engines, similar performance to native `for..of`. * Cons: Additional compilation step required. **Library usage:** None of the benchmark tests use any external libraries. **Special JS feature or syntax:** The tests utilize ECMAScript 2015 features, specifically: 1. **Arrow functions**: Used in the `forEach func` test. 2. **Transpilation**: The transpiled version of the classic `for` loop uses a modified version of the syntax to make it compatible with older engines. **Other alternatives:** If you're looking for alternative approaches to iterate over an array, consider: 1. **While loops**: A traditional loop construct that can be used with arrays. 2. **Lodash**: A popular utility library that provides various iteration functions, including `forEach`. Keep in mind that the choice of iteration approach depends on your specific use case, performance requirements, and target browser support.
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?