Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
forEach vs map logged
(version: 0)
Comparing performance of:
forEach vs map
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
forEach
const numbersArray = [32, 21, 22, 28, 26, 31, 30, 27, 28, 33, 33, 34, 25, 35, 32, 21, 22, 28, 26, 31, 30, 27, 28, 33, 33, 34, 25, 35, 32, 21, 22, 28, 26, 31, 30, 27, 28, 33, 33, 34, 25, 35, 32, 21, 22, 28, 26, 31, 30, 27, 28, 33, 33, 34, 25, 35, 32, 21, 22, 28, 26, 31, 30, 27, 28, 33, 33, 34, 25, 35, 32, 21, 22, 28, 26, 31, 30, 27, 28, 33, 33, 34, 25, 35, 32, 21, 22, 28, 26, 31, 30, 27, 28, 33, 33, 34, 25, 35, 32, 21, 22, 28, 26, 31, 30, 27, 28, 14, 33, 33, 34, 25, 35, 32, 21, 22, 28, 26, 31, 30, 27, 28, 33, 33, 34, 25, 35, 32, 21, 22, 28, 26, 31, 30, 27, 28, 33, 33, 34, 25, 35, 32, 21, 22, 28, 26, 31, 30, 27, 28, 33, 33, 34, 25, 35, 32, 21, 22, 28, 26, 31, 30, 27, 28, 33, 33, 34, 25, 35, 32, 21, 22, 28, 26, 31, 30, 27, 28, 33, 33, 34, 25, 35, 32, 21, 22, 28, 17, 26, 31, 30, 27, 28, 33, 33, 34, 25, 35, 32, 21, 22, 28, 16, 26, 31, 30, 27, 28, 33, 33, 34, 25, 35]; let resultArray = []; numbersArray.forEach(element => { return resultArray.push(element ** 2); }); console.log(resultArray);
map
const numbersArray = [32, 21, 22, 28, 26, 31, 30, 27, 28, 33, 33, 34, 25, 35, 32, 21, 22, 28, 26, 31, 30, 27, 28, 33, 33, 34, 25, 35, 32, 21, 22, 28, 26, 31, 30, 27, 28, 33, 33, 34, 25, 35, 32, 21, 22, 28, 26, 31, 30, 27, 28, 33, 33, 34, 25, 35, 32, 21, 22, 28, 26, 31, 30, 27, 28, 33, 33, 34, 25, 35, 32, 21, 22, 28, 26, 31, 30, 27, 28, 33, 33, 34, 25, 35, 32, 21, 22, 28, 26, 31, 30, 27, 28, 33, 33, 34, 25, 35, 32, 21, 22, 28, 26, 31, 30, 27, 28, 14, 33, 33, 34, 25, 35, 32, 21, 22, 28, 26, 31, 30, 27, 28, 33, 33, 34, 25, 35, 32, 21, 22, 28, 26, 31, 30, 27, 28, 33, 33, 34, 25, 35, 32, 21, 22, 28, 26, 31, 30, 27, 28, 33, 33, 34, 25, 35, 32, 21, 22, 28, 26, 31, 30, 27, 28, 33, 33, 34, 25, 35, 32, 21, 22, 28, 26, 31, 30, 27, 28, 33, 33, 34, 25, 35, 32, 21, 22, 28, 17, 26, 31, 30, 27, 28, 33, 33, 34, 25, 35, 32, 21, 22, 28, 16, 26, 31, 30, 27, 28, 33, 33, 34, 25, 35]; const resultArray = numbersArray.map(element => { return element ** 2; }); console.log(resultArray);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
forEach
map
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition** The benchmark is comparing two approaches to iterate over an array of numbers: 1. **`forEach`**: Using the `forEach` method to iterate over the array and push the squared values into a new array. 2. **`map`**: Using the `map` method to create a new array with the squared values. **Options Compared** The two options being compared are: * Iteration using `forEach` * Iteration using `map` **Pros and Cons of Each Approach:** ### `forEach` * **Pros**: * More control over each iteration (e.g., no need to return a value from the callback function) * Can be used with more complex iteration logic * **Cons**: * Requires creating an intermediate array (`resultArray`) to store the results * May have higher overhead due to the push operation ### `map` * **Pros**: * More concise and expressive way to create a new array with transformed values * Eliminates the need for an intermediate array * **Cons**: * Requires creating a new array, which can be memory-intensive * Less control over each iteration (e.g., callback function returns a value) **Benchmark Result** The latest benchmark result shows that: * `map` has a slightly higher execution frequency (`9238.9248046875 executions per second`) compared to `forEach` (`9186.7470703125 executions per second`) * Both results are relatively high, indicating efficient execution times In general, `map` is a good choice when you need to create a new array with transformed values and don't require as much control over each iteration. However, if you need more control or perform complex iteration logic, `forEach` might be a better fit. Keep in mind that these results are specific to this particular benchmark and may vary depending on other factors like the size of the input array, hardware specifications, and JavaScript engine optimizations.
Related benchmarks:
map vs forEach Chris
map vs forEach Chris v2
map vs forEach Chris v2b
JS Map foreach vs for of
Map.forEach vs Array.forEach vs Array.from(Map.prototype.values()).forEach
Comments
Confirm delete:
Do you really want to delete benchmark?