Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map vs Object vs Array @adelinolsn v1.1
(version: 1)
Comparing performance of:
Map.get vs Object
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var quantityOfElements = 1000000; var array = new Array(quantityOfElements); var quantityOfTests = 10000; for (let i = 0; i < quantityOfElements; i++) { array[i] = { id: i }; } var map = new Map(); for (let i = 0; i < quantityOfElements; i++) { map.set(array[i].id, array[i]); } var obj = {}; for (let i = 0; i < quantityOfElements; i++) { obj[array[i].id] = array[i]; }
Tests:
Map.get
for (let i = 0; i < quantityOfTests; i++) { const random = Math.floor(Math.random() * (quantityOfElements - 1)); map.get(random); }
Object
for (let i = 0; i < quantityOfTests; i++) { const random = Math.floor(Math.random() * (quantityOfElements - 1)); obj[random]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Map.get
Object
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36 Edg/126.0.0.0
Browser/OS:
Chrome 126 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Map.get
318.3 Ops/sec
Object
317.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this benchmark. **Benchmark Definition** The test is comparing the performance of three data structures: an object (`obj`), a Map (`map`), and an array (`array`). The benchmark definition creates a large dataset by generating 1 million objects with unique IDs, and then sets up three data structures: * `map`: A new instance of the Map data structure. It iterates over the dataset and adds each object to the map using its ID as the key. * `obj`: An empty object that will be used to store references to the dataset objects. * `array`: The original array created from the dataset. **Comparison Options** The benchmark is comparing two approaches: 1. **Map.get**: Iterates over the map and retrieves a random element (using the ID as the key) for a specified number of tests (10,000). 2. **Object**: Iterates over the object and accesses a random property (using the ID as the property name) for a specified number of tests (10,000). **Pros and Cons** * **Map.get**: * Pros: Maps are optimized for fast lookups using their key-value pairs. In this case, it provides direct access to any element in the map. * Cons: It may incur additional overhead due to the Map's implementation details (e.g., hash table resizing). * **Object**: * Pros: Objects provide a simple and intuitive way to store and retrieve data using property names. This approach is closer to how we typically access objects in JavaScript. * Cons: Object lookups are slower compared to the Map's direct lookup mechanism. **Library/Functionality** In this benchmark, the `Map` class is being used as a built-in JavaScript data structure. The purpose of the Map is to provide fast and efficient key-value pairs for storing and retrieving data. **Special JS Feature/Syntax** There are no special JavaScript features or syntaxes mentioned in the provided code snippet that would require additional explanation beyond the standard JavaScript basics. Now, let's consider alternative approaches: * **Array**: While arrays can be used to store collections of data, their lookups using an index (like `array[random]`) are generally slower than Map.get and Object accesses due to linear search algorithms. * **Set**: If the dataset contained unique values only, a Set could provide faster lookups compared to Objects since Sets use hash tables internally. However, in this benchmark, we're dealing with objects as keys, so it's not applicable. * **Other Libraries/Implementations**: Depending on the specific requirements and constraints of the project, other data structures like trie-based dictionaries or custom array implementations might be more suitable than the built-in JavaScript Map. By choosing between these approaches, developers can select the most efficient way to store, retrieve, and manipulate their data, depending on their specific use cases.
Related benchmarks:
Array from() vs Map.keys()
Map.forEach vs Array.forEach vs Array.from(Map.prototype.values()).forEach
Map.forEach vs Array.forEach vs Array.from(Map.values()).forEach
new Map vs Array.from vs spread operator
new Map vs set array to map
Comments
Confirm delete:
Do you really want to delete benchmark?