Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array loop vs foreach vs map big
(version: 0)
Comparing performance of:
foreach vs for vs map
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; for (var i = 0; i < 100000; i++) { arr[i] = i; } function someFn(i) { return i * 3 * 8; }
Tests:
foreach
arr.forEach(function (item){ someFn(item); })
for
for (var i = 0, len = arr.length; i < len; i++) { someFn(arr[i]); }
map
arr.map(item => someFn(item))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
foreach
for
map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 120 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
foreach
93.6 Ops/sec
for
55.6 Ops/sec
map
88.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared options, pros and cons of each approach, and other considerations. **Benchmark Overview** The benchmark is designed to compare the performance of three different approaches for iterating over an array in JavaScript: `forEach`, `for` loop, and `map`. The test case generates a large array of 100,000 elements and performs a simple calculation on each element using the `someFn` function. **Options Compared** 1. **`forEach`**: This approach uses the `Array.prototype.forEach()` method to iterate over the array. The callback function is executed for each element in the array. 2. **`for` loop**: This approach uses a traditional `for` loop to iterate over the array, keeping track of the index using the `length` property. 3. **`map`**: This approach uses the `Array.prototype.map()` method to create a new array with the results of applying the `someFn` function to each element in the original array. **Pros and Cons** 1. **`forEach`**: * Pros: concise, easy to read, and maintainable. * Cons: can be less efficient than other approaches due to the overhead of the callback function. 2. **`for` loop**: * Pros: direct control over iteration, no overhead from a library function. * Cons: more verbose and error-prone compared to `forEach`. 3. **`map`**: * Pros: concise, expressive, and easy to read. * Cons: creates a new array, which can be memory-intensive for large datasets. **Library Usage** In the benchmark code, the `someFn` function is defined outside of the loop, which implies that it's a reusable function. However, since it's not explicitly stated as a library, we assume it's a custom function defined within the test script. **Special JS Features** There are no special JavaScript features or syntax used in this benchmark that would require additional explanation. **Other Considerations** * The benchmark uses a simple calculation on each element (`i * 3 * 8`) to demonstrate performance differences between the approaches. * The `arr` array is generated dynamically using a loop, which may not be representative of real-world scenarios where arrays are pre-populated or created from other data structures. **Alternatives** Other alternatives for iterating over arrays in JavaScript include: 1. **`forEachIn`**: A custom implementation of `forEach` that uses an iterator. 2. **`for...of` loop**: A newer iteration syntax introduced in ECMAScript 2015, which is more concise and expressive than traditional `for` loops. 3. **`reduce` method**: Another array method that can be used for iterating over arrays, although it's not directly comparable to the other three approaches. These alternatives may offer better performance or expressiveness depending on the specific use case, but are not explicitly tested in this benchmark.
Related benchmarks:
Array loop vs for of loop vs foreach vs map (2)
Array loop vs foreach vs map (Small arrays)
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
Comments
Confirm delete:
Do you really want to delete benchmark?