Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map() vs Object() on write no strings
(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(i, `value${i}`) }
Object Iteration
const test = {} for (var i=100000; i > 0; i--) { test[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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Safari/605.1.15
Browser/OS:
Safari 17 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Map
134.8 Ops/sec
Object Iteration
82.9 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 the pros/cons of each approach. **Benchmark Overview** The benchmark measures the performance difference between using `Map` and `Object` data structures in JavaScript for large-scale iteration. The test creates a map or an object with 100,000 key-value pairs, where each key is a unique integer and the value is a string concatenating "value" and the key. **Test Cases** There are two individual test cases: 1. **Map**: This test case uses `Map` data structure to store key-value pairs. ```javascript const test = new Map(); for (var i=100000; i > 0; i--) { test.set(i, `value${i}`); } ``` 2. **Object Iteration**: This test case uses an object literal to store key-value pairs. ```javascript const test = {}; for (var i=100000; i > 0; i--) { test[i] = `value${i}`; } ``` **Comparison** The benchmark compares the performance of these two approaches: * **Map**: Uses a hash table data structure, which is optimized for fast lookups and inserts. * **Object Iteration**: Uses an object literal with key-value pairs, where each property is accessed using bracket notation (e.g., `test[i]`). **Pros/Cons** Here are the pros and cons of each approach: **Map** Pros: * Fast lookups and inserts due to hash table implementation * Efficient memory usage for large datasets Cons: * May have slower iteration compared to object iteration, as the browser needs to iterate over the keys and values in the map. * Requires a `Map` object creation, which may incur some overhead. **Object Iteration** Pros: * Can be faster for iteration due to direct access to properties using bracket notation * Simpler implementation and no additional object creation required Cons: * May have slower performance due to hash table lookups and inserts compared to map operations. * Memory usage can be higher due to the need to store all key-value pairs in memory. **Other Considerations** * **ES6 Map**: The `Map` data structure is a native JavaScript data structure introduced in ES6. It provides efficient methods for adding, removing, and iterating over key-value pairs. * **Browser Support**: Both Safari 17 (on Mac OS X 10.15.7) results are provided, indicating that both approaches are supported by the browser. **Other Alternatives** If you're interested in exploring alternative data structures or optimization techniques, consider the following: * **Array**: Using an array and indexing can be a viable alternative to `Map` or object iteration. * **Set**: The `Set` data structure is another native JavaScript data structure that provides efficient methods for adding and removing elements.
Related benchmarks:
Map vs Object
Map has vs get
Map vs Object - string keys - write performance
Map.set vs Object assign
Object.fromEntries vs Map
Comments
Confirm delete:
Do you really want to delete benchmark?