Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
map initialisation
(version: 0)
measure map initialization with array or map
Comparing performance of:
.forEach() vs .map()
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = [ { a: 'Ghkfj', b: 'Yvtzq', c: 'Rwsde' }, { a: 'Bpqwm', b: 'Sdfgh', c: 'Zxcvb' }, { a: 'Olmnp', b: 'Iujkl', c: 'Qwerty' }, { a: 'Cdsaf', b: 'Asdfg', c: 'Xcvbn' }, { a: 'Qwerz', b: 'Cvbnm', c: 'Xcvbn' } ]
Tests:
.forEach()
const map = new Map(); arr.forEach(el => map.set(el.a, el));
.map()
const map = new Map(arr.map(el => [el.a, el]));
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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
.forEach()
1904344.9 Ops/sec
.map()
1710895.9 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The MeasureThat.net website provides a platform for users to create and run JavaScript microbenchmarks. The provided benchmark measures the performance of initializing a Map object with either an array or a Map, using two different approaches: `.forEach()` and `.map()`. **Options Compared** Two options are compared: 1. **`.forEach()`**: This approach uses the `forEach` method to iterate over the array and set each element as a key in the Map. 2. **`.map()`**: This approach uses the `map` method to create a new array of key-value pairs, where each pair is created by mapping over the original array. **Pros and Cons** * `.forEach()`: Pros: + Easy to understand and implement + Only requires iterating over the array once Cons: + May involve additional overhead due to callback function invocation + Can lead to slower performance due to the need for an extra loop * `.map()`**: Pros: + More efficient than `.forEach()`, as it avoids the need for a separate loop + Can be faster, especially for larger arrays Cons: + Requires creating a new array, which can incur additional overhead + May not be as intuitive to understand and implement **Library/Utility Used** In this benchmark, the `Map` object is used, which is a built-in JavaScript object that stores key-value pairs. The `forEach` and `map` methods are also part of the JavaScript API. **Special JS Features/Syntax** None of the special JS features or syntax are mentioned in the provided code snippets. **Other Alternatives** If you want to explore alternative approaches, here are some options: * Using a library like Lodash, which provides a `forEach` function that can be used with Maps. * Using an array literal and then converting it to a Map using the `Object.fromEntries` method or the spread operator (`{ ... }`). * Using a for loop to iterate over the array instead of `.forEach()` or `.map()`. * Using a different data structure, such as an object or an array with a custom key-value pair mechanism. Keep in mind that these alternatives may not be directly comparable to the original benchmark, and their performance characteristics may vary depending on the specific use case.
Related benchmarks:
testsemeke
for vs map to fill array fixed
Map: Lodash vs Array.prototype
Map values to Array: Array.from vs Array Spread
flat() vs flatMap()
Comments
Confirm delete:
Do you really want to delete benchmark?