Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Fastest iteration over array: map vs forEeach vs while vs for loop (fixed)
(version: 0)
Comparing performance of:
for loop vs forEach vs map vs while
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
window.arr = [1, "two", 3, "four", 5, {"sixth": 6}, 7, 8, 9, "ten"];
Tests:
for loop
for (let i = 0; i < arr.length; i++) console.log(arr[i]);
forEach
arr.forEach(el => console.log(el));
map
arr.map(el => console.log(el));
while
let i = 0; while (i < arr.length) { console.log(arr[i]); i++; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
for loop
forEach
map
while
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/115.0
Browser/OS:
Firefox 115 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for loop
19064.9 Ops/sec
forEach
17749.4 Ops/sec
map
18171.6 Ops/sec
while
16112.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview of the Benchmark** The provided JSON represents a JavaScript microbenchmark, specifically measuring the performance of different iteration methods over an array: `map`, `forEach`, `for` loop, and `while`. The benchmark is designed to compare the execution speeds of these four approaches in Firefox 115 on a Windows desktop. **Options Compared** 1. **Map**: Uses the `map()` method to create a new array with the results of applying a provided function to each element of the original array. 2. **ForEach**: Uses the `forEach()` method to iterate over the array and execute a provided function for each element. 3. **For Loop**: Uses a traditional `for` loop to iterate over the array, accessing each element using an index variable. 4. **While Loop**: Uses a `while` loop to iterate over the array, incrementing an index variable until it reaches the length of the array. **Pros and Cons** * **Map**: + Pros: More concise and expressive code, as the transformation function is applied directly to each element. + Cons: Creates a new array, which can lead to performance overhead if the original array is large. * **ForEach**: + Pros: Concise and expressive code, as the callback function is executed for each element without mutating the original array. + Cons: Also creates a new scope for the callback function, which can impact performance. * **For Loop**: + Pros: Traditional approach that many developers are familiar with, avoiding any potential issues related to `map` or `forEach`. + Cons: Requires manual index management and loop termination, making it less concise than `map` and `forEach`. * **While Loop**: + Pros: Simple and efficient, as the loop only iterates over the array once. + Cons: Can be more verbose than other approaches, requiring explicit index management. **Library Usage** None of the provided benchmark definitions explicitly use a library. However, it's worth noting that `map` and `forEach` rely on the Array prototype methods, which are part of the ECMAScript standard. **Special JS Features or Syntax** None of the provided benchmark definitions explicitly use any special JavaScript features or syntax beyond what is required for basic iteration. **Other Alternatives** For similar performance benchmarks: 1. **Benchmarking frameworks**: Tools like Benchmark.js, Benchmarck, or jsPerf can help automate and standardize benchmarking. 2. **ES6+ methods**: Consider using newer methods like `reduce()`, `every()`, or `some()` for array iteration, which might provide better performance in certain cases. Keep in mind that the specific results of this benchmark may vary depending on the version of JavaScript, browser, and platform used.
Related benchmarks:
forEach vs for vs while
For and Map
Array loop vs foreach vs map with large array
Fastest iteration over array: map vs forEeach vs while vs for loop
Comments
Confirm delete:
Do you really want to delete benchmark?