Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
index loop vs for-of loop vs foreach vs map
(version: 0)
Comparing performance of:
foreach vs for vs map vs for-of
Created:
7 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; for (var i = 0; i < 1000; i++) { arr[i] = i; } function someFn(i) { return i * 3 * 8; }
Tests:
foreach
arr.forEach(someFn)
for
for (var i = 0, len = arr.length; i < len; i++) { someFn(arr[i]); }
map
arr.map(someFn)
for-of
for (const i of arr) { someFn(arr[i]); }
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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
foreach
160458.2 Ops/sec
for
417786.1 Ops/sec
map
135587.6 Ops/sec
for-of
394257.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**What is being tested?** The provided JSON represents a JavaScript microbenchmark that measures the execution time of different loop constructs on an array: `forEach`, `for` loops, `map`, and `for-of` loops. **Options compared** The benchmark compares four different approaches: 1. **forEach**: The `forEach` method is called on each element of the array, with a callback function that performs the desired operation. 2. **For loop**: A traditional `for` loop is used to iterate over the elements of the array, with an index variable `i` and a length variable `len`. 3. **Map**: The `map()` method is called on the array, which returns a new array with the results of applying the provided function to each element. 4. **For-of loop**: A `for...of` loop is used to iterate over the elements of the array, without an explicit index variable. **Pros and Cons** * **forEach**: Pros: Simple to implement, efficient for small arrays. Cons: May not be as efficient as other methods for large arrays due to function call overhead. * **For loop**: Pros: Allows direct access to array elements, can be faster for large arrays. Cons: Requires manual indexing and length management, can be error-prone. * **Map**: Pros: Returns a new array with the results, efficient for large arrays. Cons: Creates an additional array, may not be suitable for small arrays where memory is a concern. * **For-of loop**: Pros: Concise, easy to read, and maintainable. Cons: May have slightly slower performance compared to traditional `for` loops. **Library** There is no library explicitly used in this benchmark. However, the `map()` method uses the built-in JavaScript array prototype methods. **Special JS features or syntax** The benchmark does not use any special JavaScript features or syntax other than the four loop constructs being tested.
Related benchmarks:
Array loop: forEach vs for vs map vs for of entries
Array loop vs foreach vs map -2
Array loop vs foreach vs map with large array
Array loop vs for of loop vs foreach vs map fixed
Comments
Confirm delete:
Do you really want to delete benchmark?