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; 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];
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 on MeasureThat.net. **Benchmark Overview** The provided benchmark compares the performance of different ways to iterate over an array in JavaScript. The test case creates an array of 10,000 elements and uses the `forEach` method to sum up all the values. **Tested Options** 1. **`forEach`**: This is a built-in JavaScript method that iterates over an iterable (like an array) and executes a callback function for each element. 2. **`for...of`**: This is another built-in JavaScript loop construct that allows you to iterate over arrays and other iterable objects. 3. **`forEach func`** (also known as "arrow function `forEach`"): This is similar to the regular `forEach`, but uses an arrow function instead of a traditional function declaration. 4. **`for...of transpiled`**: This is a transpiled version of the `for...of` loop, which means it's been converted from modern JavaScript syntax to older syntax that's compatible with older browsers. **Pros and Cons** * **`forEach`**: + Pros: concise, readable, and widely supported. + Cons: can be slower than other methods due to overhead of the callback function. * **`for...of`**: + Pros: generally faster than `forEach`, especially for large arrays. + Cons: may not be as readable or familiar to some developers who haven't used it before. * **`forEach func` (arrow function `forEach`)**: + Pros: concise, readable, and can be faster than regular `forEach` due to the arrow function's optimized behavior. + Cons: might be less familiar to older developers. * **`for...of transpiled`**: + Pros: compatible with older browsers that don't support modern syntax. + Cons: may not be as readable or efficient as other methods. **Library and Special Features** In this benchmark, there is no specific library used. However, the `forEach` method relies on the built-in `Array.prototype.forEach()` function, which is a part of the ECMAScript standard. There are no special JavaScript features or syntax used in this benchmark, beyond what's mentioned above (e.g., arrow functions, transpiled code). **Other Alternatives** If you want to explore other ways to iterate over arrays in JavaScript, here are some alternatives: * **`for...in`**: This loop construct iterates over the properties of an object, but can be used with arrays by passing a numeric argument (e.g., `arr[0], arr[1], ...`). * **`map()`, `filter()`, and other array methods**: These methods return new arrays or arrays-like objects after applying a transformation function to each element. While not traditional loops, they can be used for iterative processing. * **`reduce()`**: This method applies a reduction function to an array, accumulating a result from the first element to the last. These alternatives might offer different performance profiles or trade-offs in terms of readability and maintainability, depending on your specific use case.
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?