Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
foreach vs map by wayne
(version: 13)
Benchmark.js
Comparing performance of:
foreach vs map
Created:
4 years ago
by:
Registered User
Go to the latest result
Tests:
foreach
let arr = Array(10000); arr.fill({name: 'ddd', age: 123}); const newArr1 = [] const newArr2 = [] arr.forEach((v) => { newArr1.push(v.name) newArr2.push(v.age) })
map
let arr = Array(10000); arr.fill({name: 'ddd', age: 123}); const newArr2 = [] const newArr1 = arr.map((v) => { newArr2.push(v.age) return v.name })
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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:133.0) Gecko/20100101 Firefox/133.0
Browser/OS:
Firefox 133 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
foreach
5K/s
map
4K/s
View exact numbers
Test name
Executions per second
✓
foreach
5,254 Ops/sec
map
4,219 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON data and explain what's being tested, compared, and some of the considerations involved. **Benchmark Definition** The provided benchmark is called "foreach vs map by wayne". It's a simple JavaScript microbenchmark that compares the performance of two approaches: 1. Using `forEach` loop to iterate over an array and push values into two separate arrays. 2. Using `map()` function to create a new array with transformed values. **Script Preparation Code** The script preparation code is empty, which means the benchmarker doesn't need to perform any initialization or setup before running the tests. **Html Preparation Code** Similarly, the HTML preparation code is also empty, indicating that no specific HTML structure needs to be generated for this benchmark. **Individual Test Cases** There are two test cases: 1. **foreach**: This test case uses a `forEach` loop to iterate over an array and push values into two separate arrays (`newArr1` and `newArr2`). The `forEach` callback function has access to the current value of each element in the original array, which is stored in the variable `v`. 2. **map**: This test case uses the `map()` function to create a new array with transformed values. The `map()` callback function returns the transformed value for each element in the original array. **Test Results** The latest benchmark result shows that the `map()` approach outperforms the `forEach` approach on this particular benchmark, with 2.25 times more executions per second (9161.1337890625 vs 4062.207275390625). **Comparison Options** Here's a brief summary of the options being compared: * **forEach**: Iterates over an array using a traditional loop, pushing values into two separate arrays. + Pros: - Easy to read and maintain for developers familiar with loops. - Can be used when working with legacy code or libraries that don't support `map()`. + Cons: - Less efficient than `map()` due to the need to create intermediate arrays. * **map**: Creates a new array with transformed values, using an iterator-like interface. + Pros: - More concise and expressive than traditional loops. - Can be more efficient than `forEach` since it avoids creating intermediate arrays. + Cons: - Less intuitive for developers unfamiliar with functional programming concepts. **Library: Lodash (not explicitly used in this benchmark)** While not explicitly mentioned, the use of `forEach()` and `map()` suggests that the developer is familiar with JavaScript's built-in array methods. If a library like Lodash were to be used in this benchmark, it might introduce additional overhead or alter the results. **Special JS Feature/Syntax** There are no special JavaScript features or syntaxes being tested in this benchmark. **Alternatives** Some alternative approaches that could have been explored in this benchmark include: * Using `filter()` to create a new array with filtered values. * Using `reduce()` to accumulate values into an object. * Using a custom iterator or generator function. * Using a parallel processing approach, such as using Web Workers. Keep in mind that the choice of approach depends on the specific requirements and constraints of the project.
Related benchmarks
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?