Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Speed test of for of and for each
(version: 0)
Comparing performance of:
for of vs for Each
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var odd = []; var even = []; var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
Tests:
for of
for (const item of array) { if (item % 2 !== 0) { odd.push(item); } else { even.push(item); } }
for Each
array.forEach(item => { if (item % 2 !== 0) { odd.push(item); } else { even.push(item); } })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for of
for Each
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):
I'd be happy to explain what's being tested in the provided JSON benchmark. **Benchmark Overview** The test compares two approaches for iterating over an array: the `for...of` loop and the `forEach()` method. Both methods are used to separate even and odd numbers from a given array of integers. **Options Being Compared** 1. **`for...of` Loop**: This is an iterative syntax that allows you to iterate over arrays, objects, or iterables using a single loop. 2. **`forEach()` Method**: This is a built-in method for iterating over arrays and other iterable objects. **Pros and Cons of Each Approach** 1. **`for...of` Loop**: * Pros: + Can be used with more complex data structures, like objects or arrays with holes. + Allows you to access both the value and its key (in the case of an object). * Cons: + Can be slower for large arrays due to the overhead of creating a loop counter variable. 2. **`forEach()` Method**: * Pros: + Faster for large arrays since it's optimized for iteration. + Allows for more concise code, as you don't need to declare a separate loop variable. * Cons: + Can only be used with arrays or other iterable objects. + Does not allow direct access to the value being iterated over. **Library Usage** There is no library explicitly mentioned in the benchmark. However, some browsers may use internal libraries or engines that provide optimized implementations of these iteration methods. **Special JavaScript Features or Syntax** The `for...of` loop and `forEach()` method are standard features in modern JavaScript, introduced in ECMAScript 2015 (ES6) and later versions. They are widely supported by most browsers and Node.js environments. **Other Considerations** * The benchmark measures the number of executions per second for each iteration method. * The test is run on a desktop platform with Chrome 118 browser. * The results show that `forEach()` is faster than `for...of` in this specific scenario, although the difference is relatively small. **Alternative Approaches** Other methods you could use to iterate over an array include: 1. **Traditional Loops**: Using `while`, `do-while`, or `for` loops with index variables. 2. **`map()` and `reduce()` Methods**: These can be used in combination to achieve similar results, although they may have different performance characteristics. Keep in mind that these alternative approaches might not be as efficient or concise as the `for...of` loop and `forEach()` method, which are optimized for modern JavaScript engines.
Related benchmarks:
remove with splice vs filter
Array concat speed
Array.slice(-1) vs Array.At(-1) Teste Pro Eduzeira
Add element at index 0 (Splice vs Spread vs unshift)
for vs every simple
Comments
Confirm delete:
Do you really want to delete benchmark?