Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map() vs Object() on write
(version: 0)
Comparing performance of:
Map vs Object Iteration
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Map
const test = new Map() for (var i=100000; i > 0; i--) { test.set(`key${i}`, `value${i}`) }
Object Iteration
const test = {} for (var i=100000; i > 0; i--) { test[`key${i}`] = `value${i}` }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Map
Object Iteration
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/123.0.0.0 Safari/537.36
Browser/OS:
Chrome 123 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Map
48.9 Ops/sec
Object Iteration
48.2 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. **What is being tested?** The benchmark compares two approaches: 1. **Map()**: The first test case uses a `Map` object to store key-value pairs. It iterates 100,000 times, setting a new key-value pair for each iteration. 2. **Object Iteration (using bracket notation)**: The second test case uses an object literal to store key-value pairs. It also iterates 100,000 times, but this time using bracket notation (`test['key${i}'] = 'value${i}'`) to access and update the object properties. **Options compared** The benchmark is comparing two options: * **Map()**: Using a `Map` object for storing key-value pairs. * **Object Iteration (using bracket notation)**: Using an object literal with bracket notation to store key-value pairs. **Pros and Cons of each approach** * **Map()** + Pros: - Faster lookups, insertions, and deletions compared to objects - Provides a way to iterate over the entries in the map using the `forEach()` method or the spread operator (`[...map]`) + Cons: - Can be slower for small datasets due to the overhead of creating and managing a hash table * **Object Iteration (using bracket notation)** + Pros: - Can be faster than Map() for small datasets, as objects are stored in memory using a single data structure + Cons: - Requires explicit indexing and concatenation, which can lead to slower performance due to the creation of temporary strings **Library usage** Neither of these approaches uses any external libraries. The `Map` object is a built-in JavaScript data structure, while object literals are also a native part of the language. **Special JS feature or syntax** This benchmark does not utilize any special JavaScript features or syntax beyond what's required for the test cases themselves. It only demonstrates how to use `Map` and objects with bracket notation in different ways. **Alternative approaches** Other alternatives for storing key-value pairs could include: * Using an array of objects, where each object represents a single key-value pair * Utilizing a library like Lodash or Underscore.js, which provide optimized data structures and methods for working with arrays and objects Keep in mind that the choice of approach depends on the specific requirements and constraints of your use case. For example, if you need to store large amounts of data or perform frequent lookups, `Map()` might be a better option. However, if you're working with small datasets and prioritize simplicity, object iteration using bracket notation could be sufficient. It's worth noting that MeasureThat.net also provides a way to compare these approaches in a more comprehensive manner by running them against other variants, such as `for...of` loops or even external libraries like Ramda.
Related benchmarks:
Map vs Object - string keys - write performance
Map vs object for deletions
new Map vs set array to map
Array Spread vs Fill vs New Array
Object.fromEntries vs Map
Comments
Confirm delete:
Do you really want to delete benchmark?