Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs forEach vs forOf 2 part
(version: 0)
for vs forEach vs forOf without iptimization by engine
Comparing performance of:
for vs forEach vs forOf
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; for(let i = 0; i<10_000; i++) { arr.push(Math.random()); }
Tests:
for
let acc = 0; for(let i=0; i<arr.length; i++){ acc+=arr[i] * Date.now(); } const lie = acc%3===0 ? acc : acc + '';
forEach
let acc = 0; arr.forEach(e=>{ acc+=e * Date.now(); }) const lie = acc%3===0 ? acc : acc + '';
forOf
let acc = 0; for(const e of arr) { acc+=e * Date.now(); } const lie = acc%3===0 ? acc : acc + '';
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
for
forEach
forOf
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):
**Overview** The provided JSON represents a JavaScript benchmark that compares the performance of three different approaches: traditional `for` loops, `forEach`, and `for...of`. The test cases aim to measure how these approaches execute in various scenarios. **What is tested?** * Three `for` loop variants: + Traditional `for` loop with an incrementing variable (`i`) + `forEach` method (a built-in array method that iterates over elements) + `for...of` loop (a newer syntax introduced in ECMAScript 2015, allowing iteration without explicit indices) * The execution time and performance of each approach on a randomly generated array of 10,000 elements. **Options compared** The benchmark compares the execution time and performance of three different approaches: 1. **Traditional `for` loop**: This is the most common and widely supported way to iterate over arrays in JavaScript. 2. **`forEach` method**: A built-in array method that allows iterating over elements without explicit indices. It's often used for its concise syntax and ease of use. 3. **`for...of` loop**: A newer syntax introduced in ECMAScript 2015, which allows iteration without explicit indices. **Pros and cons** * Traditional `for` loop: + Pros: widely supported, easy to understand, and well-established. + Cons: can be verbose, especially for complex iterations. * `forEach` method: + Pros: concise syntax, easy to use, and built-in support in most browsers. + Cons: may not perform as well as traditional loops due to overhead from the method invocation. * `for...of` loop: + Pros: concise and expressive syntax, allowing for more natural iteration patterns. + Cons: relatively new syntax, which might require additional time to learn and implement. **Library** In this benchmark, no libraries are used. The test cases rely solely on built-in JavaScript features. **Special JS feature or syntax** None of the provided code snippets use any special JavaScript features or syntax beyond what is already mentioned (i.e., `for`, `forEach`, and `for...of` loops).
Related benchmarks:
Fill array with random integers
Preinitialized array size vs Push operations to an empty one.
Array .push() vs .unshift() with random numbers
Iterator vs Index for loop (cached array length fixed)
Array push or set
Comments
Confirm delete:
Do you really want to delete benchmark?