Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object vs Map
(version: 0)
Comparing performance of:
Map set vs Object set vs Map read vs Object read
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function randomString() { var text = ""; var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; for( var i=0; i < 5; i++ ) text += possible.charAt(Math.floor(Math.random() * possible.length)); return text; } const map = new Map(); const obj = {}; const map2 = new Map(); const obj2 = {}; for (let i = 0; i < 1000; i++) { const key = randomString(); const val = randomString(); map2.set(key,val); obj2[key] = val; }
Tests:
Map set
for (let i=0; i < 1000; i++) { const key = randomString(); const val = randomString(); map.set(key,val); }
Object set
for (let i=0; i < 1000; i++) { const key = randomString(); const val = randomString(); obj[key] = val; }
Map read
for (let key of map2.keys()) { const val = map2.get(key); }
Object read
for (let key in obj2) { const val = obj2[key]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Map set
Object set
Map read
Object read
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Map set
1291.0 Ops/sec
Object set
971.5 Ops/sec
Map read
17003.4 Ops/sec
Object read
11823.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll do my best to break down the benchmark and explain what's being tested. **Benchmark Overview** The Object vs Map benchmark compares the performance of using objects (specifically, JavaScript objects) versus Maps (a built-in JavaScript data structure) for storing and retrieving key-value pairs. The benchmark creates a large dataset with 1000 random key-value pairs and then measures the execution time for different operations: setting values in an object or a map, reading values from an object or a map. **Options Compared** The benchmark compares two options: 1. **Object Set**: This option uses JavaScript objects to store and retrieve key-value pairs. 2. **Map Set**: This option uses Maps (a built-in JavaScript data structure) to store and retrieve key-value pairs. **Pros and Cons of Each Approach** * **Object Set**: + Pros: Objects are widely supported across different browsers and platforms, making it a good choice for compatibility. + Cons: Objects can be slower than Maps due to the need for explicit property lookups using `in` or bracket notation (`obj[key] = val;`). This can lead to additional overhead in terms of computation and memory usage. * **Map Set**: + Pros: Maps are optimized for fast lookup, insertion, and deletion operations, making them generally faster than objects for these use cases. Additionally, Maps provide a `forEach` method for iterating over key-value pairs, which can simplify code. + Cons: Maps are not as widely supported across different browsers and platforms as objects, so developers may need to consider compatibility issues. **Library Usage** The benchmark uses the following libraries: * None explicitly mentioned in the provided code. However, it's worth noting that some JavaScript engines (like V8) might be using some internal data structures or optimized implementations for Maps. **Special JS Feature/Syntax** None explicitly mentioned in the provided code. **Other Considerations** When choosing between objects and Maps, consider the following factors: * **Data structure size**: If you need to store a large amount of key-value pairs, Maps might be a better choice due to their optimized performance for lookup operations. * **Read-heavy vs write-heavy workloads**: If your application is primarily read-heavy (e.g., displaying data in a UI), objects might be sufficient. However, if your application is mostly write-heavy (e.g., updating data frequently), Maps are likely to provide better performance. * **Browser and platform compatibility**: If you need to support older browsers or platforms, using objects might be a safer choice due to their wider compatibility. **Alternatives** Other alternatives for storing and retrieving key-value pairs include: * **Array**: While not ideal for lookup operations, arrays can be used for simple key-value storage. However, they lack the optimized performance of Maps. * **IndexedDB**: A client-side database that provides a more structured way to store data than objects or Maps. It's designed for larger-scale applications and requires more setup than objects or Maps. * **Other libraries or frameworks**: Depending on your specific use case and requirements, other libraries like React Query, Redux, or Vuex might provide more suitable alternatives for managing key-value pairs. Keep in mind that this analysis is based on the provided benchmark code and might not cover all possible scenarios. It's essential to evaluate your specific use case and requirements before choosing a data structure or library.
Related benchmarks:
Large Map vs Object 2
Object vs Map 5
comparing Map and object
Map vs object copying
Comments
Confirm delete:
Do you really want to delete benchmark?