Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array loops: for vs for in vs for of vs forEach vs map [fixed for IE]
(version: 10)
Comparing performance of:
for vs for in vs for of vs forEach vs map
Created:
7 years ago
by:
Registered User
Go to the latest result
Script Preparation code:
var arr = []; for (let i = 0; i < 1000; i++) { arr[i] = i; } function foo(bar) { return bar; }
Tests:
for
for (let i = 0, len = arr.length; i < len; i++) { foo(arr[i]); }
for in
for (let i in arr) { foo(arr[i]); }
for of
for (let val of arr) { foo(val); }
forEach
arr.forEach(function(val) { foo(val); });
map
arr.map(function(val) { return foo(val); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
for
for in
for of
forEach
map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.127 Safari/537.36 OPR/60.3.3004.55692
Browser/OS:
Opera 60 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
for of
32K/s
for
19K/s
forEach
6K/s
map
4K/s
for in
3K/s
View exact numbers
Test name
Executions per second
✓
for of
31,795 Ops/sec
for
19,259 Ops/sec
forEach
5,908 Ops/sec
map
4,446 Ops/sec
for in
2,645 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested, compared, and their pros and cons. **Benchmark Definition** The benchmark measures the performance of different JavaScript loops for iterating over an array: 1. `for` loop 2. `for in` loop (which is deprecated) 3. `for of` loop (introduced in ECMAScript 2015) 4. `forEach` method 5. `map` method **Options Compared** The five options are compared to determine which one is the fastest for iterating over an array. Here's a brief overview of each option: * **`for` loop**: This is the traditional way of looping over an array in JavaScript. It uses a manual index variable (`i`) and checks the bounds of the array. + Pros: Easy to understand, widely supported. + Cons: Can be slower than other options due to the manual indexing. * `for in` loop: This option is deprecated and should not be used for iterating over arrays. It iterates over the properties (keys) of the object, not its values. + Pros: None. + Cons: Slower, less reliable, and considered bad practice. * `for of` loop: Introduced in ECMAScript 2015, this loop is designed specifically for iterating over iterable objects like arrays. + Pros: Faster, more efficient, and modern. + Cons: Requires ECMAScript 2015 support. * `forEach` method: This is a built-in method on arrays that allows you to execute a callback function for each element in the array. + Pros: Easy to use, widely supported. + Cons: Can be slower than other options due to the overhead of the method call. * `map` method: Another built-in method on arrays, `map` returns a new array with the results of applying a provided function to each element in the original array. + Pros: Convenient for transforming data, widely supported. + Cons: Can be slower than other options due to the overhead of the method call. **Library and Special JS Features** None of the test cases use any external libraries or special JavaScript features. They only rely on built-in JavaScript functionality. **Other Alternatives** If you're looking for alternative ways to iterate over arrays, some options include: * Using `reduce()` method * Using `every()` and `some()` methods * Implementing a custom loop using recursion or a generator function Keep in mind that the performance of these alternatives may vary depending on the specific use case and JavaScript engine. **Benchmark Preparation Code** The provided benchmark preparation code sets up an array with 1000 elements, each initialized to its index value. It also defines a simple `foo` function that takes a single argument and returns it. **Individual Test Cases** Each test case has a unique benchmark definition that represents the specific loop being tested. The `Test Name` field provides a clear indication of which loop is being measured.
Related benchmarks
Array loop vs for of loop vs foreach vs map (2)
Array loop: forEach vs for vs map vs for of entries
Array loop vs foreach vs map with large array
Comments
Confirm delete:
Do you really want to delete benchmark?