Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
loops comparision
(version: 0)
Comparing performance of:
foreach vs for vs map vs for of
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
foreach
const numbers = [1, 2, 3, 4, 5]; numbers.forEach(number => { console.log(number); });
for
const numbers = [1, 2, 3, 4, 5]; for (let i = 0; i < numbers.length; i++) { console.log(numbers[i]); }
map
const numbers = [1, 2, 3, 4, 5]; const squares = numbers.map(number => number * number); console.log(squares);
for of
const numbers = [1, 2, 3, 4, 5]; for (const number of numbers) { console.log(number); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
foreach
for
map
for of
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; SAMSUNG SM-J810G) AppleWebKit/537.36 (KHTML, like Gecko) SamsungBrowser/16.0 Chrome/92.0.4515.166 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 92 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
foreach
2732.8 Ops/sec
for
2522.1 Ops/sec
map
8823.4 Ops/sec
for of
2201.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark data and explain what's being tested. **What is being tested?** MeasureThat.net is testing the performance of three different ways to iterate over an array in JavaScript: `forEach`, `for` loop, `map`, and `for...of` loop. The benchmark is comparing the execution speed of these four approaches on a fixed-size array. **Options compared:** 1. **forEach**: Uses the `Array.prototype.forEach()` method, which calls a provided callback function for each element in the array. 2. **For loop**: Uses a traditional `for` loop to iterate over the array elements. 3. **Map**: Uses the `Array.prototype.map()` method, which creates a new array with the results of applying a provided function to each element in the original array. 4. **For...of loop**: Uses the `for...of` loop, which is a newer iteration syntax that allows iterating over arrays and other iterable objects. **Pros and cons:** 1. **forEach**: * Pros: Simple and concise, easy to read and write. * Cons: Can be slower than traditional loops for large datasets due to the overhead of the callback function invocation. 2. **For loop**: * Pros: Control over iteration variables, potential for better cache locality. * Cons: More verbose and error-prone compared to other options. 3. **Map**: * Pros: Creates a new array with transformed data, potentially useful for pipeline processing. * Cons: Can be slower than traditional loops due to the overhead of creating a new array and applying the transformation function. 4. **For...of loop**: * Pros: Concise and readable, allows iterating over arrays without explicit indexing. * Cons: Less control over iteration variables compared to traditional `for` loops. **Library usage:** None of the benchmarked code snippets use any external libraries. However, it's worth noting that some browsers may have internal optimizations or extensions that could affect the performance results. **Special JS feature or syntax:** There is no specific JavaScript feature or syntax being tested in this benchmark. The focus is on comparing the performance of different iteration approaches. **Other alternatives:** If you're interested in exploring other options, here are a few: 1. **Reduce**: Instead of `forEach`, you could use `Array.prototype.reduce()` to accumulate values. 2. **Array.prototype.every()`, `Array.prototype.some()`: These methods can be used for conditional iteration, but may have performance implications due to the overhead of checking conditions for each element. 3. **Callback-based iteration**: You could also explore using callback-based iteration with `setInterval()` or `setTimeout()` to test the performance of different iteration approaches. Keep in mind that the specific results may vary depending on the JavaScript engine and browser being used, as well as other factors such as system resources and network connectivity.
Related benchmarks:
Object iteration vs Map iteration
1 vs x loop iterators
Object iteration vs Map iteration V2
Multi dimensional array loops 15
Iteration Array for-of vs Map for-of 5
Comments
Confirm delete:
Do you really want to delete benchmark?