Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Test foreach, for, map
(version: 0)
Test For
Comparing performance of:
foreach vs for vs map
Created:
7 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; for (var i = 0; i < 1000000; 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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
foreach
27.2 Ops/sec
for
15.1 Ops/sec
map
26.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what is being tested in the provided JSON. **What is being tested?** The website MeasureThat.net is testing three different approaches for iterating over an array of numbers: 1. **`forEach`**: The `forEach` method is called on an array, and a function is executed for each element in the array. 2. **`for` loop**: A traditional `for` loop is used to iterate over the array, with a variable `i` that increments from 0 to the length of the array minus 1. 3. **`map` method**: The `map` method is called on an array, and a new array is returned with the results of applying a given function to each element in the original array. **Options compared** The three options are compared in terms of their execution time for the same set of iterations (in this case, 1 million elements). **Pros and cons of each approach** * **`forEach`**: Pros: concise, easy to read, and maintain. Cons: not as efficient as traditional loops since it involves a function call overhead. * **`for` loop**: Pros: control over the iteration process, no function call overhead. Cons: more verbose than `forEach`, requires manual management of the loop variable. * **`map` method**: Pros: concise and expressive, easy to write and read. Cons: creates a new array, which can be memory-intensive for large datasets. **Library used** The `someFn` function is used in each benchmark definition. This function multiplies its input by 3 and then by 8, effectively multiplying the input by 24. The purpose of this function is to demonstrate how the iteration approaches affect the execution time of the multiplication operation. **Special JS feature or syntax** There are no special JavaScript features or syntax used in these benchmark definitions. **Other alternatives** If you wanted to test other iteration approaches, some options might include: * Using `forEach` with a callback function * Using `reduce` method instead of `map` * Implementing custom iteration logic using a while loop or recursive functions Keep in mind that the choice of iteration approach depends on the specific use case and requirements. MeasureThat.net's focus is on demonstrating the relative performance of different iteration methods, rather than exploring alternative approaches.
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 e
Array loop vs foreach vs map with large array
Array map vs forEach
Comments
Confirm delete:
Do you really want to delete benchmark?