Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
map vs object 2 2 2 2
(version: 0)
Comparing performance of:
map vs object
Created:
2 years ago
by:
Registered User
Jump to the latest result
Tests:
map
var map = new Map() var key1 = String(Math.random()) var key2 = String(Math.random()) map.set(key1, key1) map.set(key2, key2) var times = 8 while (times--) { map.get(Math.random() > 0.5 ? key1 : key2) }
object
var obj = {} var key1 = String(Math.random()) var key2 = String(Math.random()) obj[key1] = key1 obj[key2] = key2 var times = 8 while (times--) { obj[Math.random() > 0.5 ? key1 : key2] }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
map
object
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 17_1_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.1.2 Mobile/15E148 Safari/604.1
Browser/OS:
Mobile Safari 17 on iOS 17.1.2
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
map
1113292.6 Ops/sec
object
777810.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **Benchmark Definition** The benchmark definition is a JSON object that contains two identical test cases: `map` and `object`. The purpose of this benchmark is to compare the performance of using a `Map` data structure versus an object literal (`{}`) to store key-value pairs in JavaScript. **Script Preparation Code** There is no script preparation code provided, which means that the test cases are designed to be executed directly in the browser or Node.js environment without any additional setup. **Html Preparation Code** There is also no html preparation code provided, which suggests that the benchmark only runs JavaScript tests and does not involve rendering HTML content. **Individual Test Cases** ### Map The `map` test case creates a new instance of the `Map` class and sets two key-value pairs: `key1` and `key2`, both with the same value as their keys. The test then enters a loop that executes the following code 8 times: * Retrieves a random key from either `key1` or `key2` using the `get()` method. * The retrieval is done randomly, depending on whether `Math.random() > 0.5`. ### Object The `object` test case creates an empty object literal and sets two key-value pairs: `key1` and `key2`, both with the same value as their keys. The test then enters a loop that executes the following code 8 times: * Retrieves a random key from either `key1` or `key2` using bracket notation (`obj[key]`). * The retrieval is done randomly, depending on whether `Math.random() > 0.5`. **Pros and Cons** The main difference between these two approaches is how the keys are looked up: * In the `map` test case, the keys are accessed using the `get()` method, which returns a value or throws an error if the key is not found. * In the `object` test case, the keys are accessed using bracket notation (`obj[key]`), which directly returns the corresponding value. **Pros of using Map:** * Faster lookup times since `Map` uses a hash table data structure under the hood. * More explicit control over whether to return a value or throw an error when a key is not found. **Cons of using Map:** * May require additional type checking or handling for cases where the returned value needs to be treated as a specific type. **Pros of using Object Literal:** * Familiar syntax and semantics for most developers. * No additional overhead or configuration required. **Cons of using Object Literal:** * Slower lookup times due to linear search through the object's property list. * May lead to unexpected behavior if not handled carefully (e.g., null, undefined keys). **Library and Purpose** The `Map` class is a built-in JavaScript data structure introduced in ECMAScript 2015. Its primary purpose is to provide fast and efficient key-value pair storage, allowing for easy iteration and lookup of values. **Special JS Feature or Syntax** There are no special JavaScript features or syntax used in these test cases beyond the use of `Map` and object literals.
Related benchmarks:
Foreach&Push vs Map2
flatMap vs map/flat
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?