Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
forEach.Map.set() vs new Map()
(version: 0)
Comparing performance of:
Set individually vs Create with properties
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var chars = [...'abcdefghijklmnopqrstuvwxyz']; function setIndividually(arr) { return arr.reduce((acc, cur) => { acc.set(cur, { char: cur }); return acc; }, new Map()); } function createWithProperties(arr) { const props = arr.map(elem => [elem, { char: elem }]); return new Map(props); }
Tests:
Set individually
var foo = setIndividually(chars);
Create with properties
var bar = createWithProperties(chars);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Set individually
Create with properties
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/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set individually
312072.2 Ops/sec
Create with properties
266785.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, and their pros and cons. **Benchmark Definition** The test case compares two approaches to create and populate a Map data structure in JavaScript: 1. `setIndividually(arr)`: This function creates a Map by iteratively setting individual key-value pairs using the `set()` method. 2. `createWithProperties(arr)`: This function creates a Map by passing an array of key-value pairs directly to the constructor. **What's being compared** The test case compares the performance (in terms of executions per second) of these two approaches on a sample dataset (`chars`) containing 26 characters. **Pros and Cons** 1. `setIndividually(arr)`: * Pros: + More explicit and controlled way of setting key-value pairs. + Could be more suitable for certain use cases where individual settings are needed. * Cons: + Might lead to slower performance due to the overhead of repeated `set()` calls. 2. `createWithProperties(arr)`: * Pros: + Potentially faster since it uses a single constructor call with an array of key-value pairs. + More concise and easier to read. * Cons: + Assumes that the properties can be passed directly in the order they should be set, which might not always be the case. **Library/Tool usage** There is no explicit library or tool being used in this benchmark. However, it's worth noting that `Map` is a built-in JavaScript data structure introduced in ECMAScript 2015 (ES6). **Special JS feature/syntax** No special JavaScript features or syntax are mentioned. The code only uses standard JavaScript features like `Map`, arrays, and arrow functions. **Other alternatives** Alternative approaches to create and populate a Map could include: 1. Using the `Object.create()` method to create an object with initial properties. 2. Using the `Array.prototype.map()` method to create a new array of key-value pairs. 3. Using a library like Lodash or Ramda for functional programming. However, these alternatives might not be as concise or efficient as the `createWithProperties(arr)` approach used in this benchmark.
Related benchmarks:
Object.defineProperty vs Object.assign vs key set
Object.defineProperty vs Object.assign vs. Direct property assignment
Object.defineProperty vs Object.assign vs direct assigment
Object.fromEntries vs reduce vs property assignment vs Map
Comments
Confirm delete:
Do you really want to delete benchmark?