Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map vs Object vs Array @adelinolsn delete
(version: 0)
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.delete(random); }
Object
for (let i = 0; i < quantityOfTests; i++) { const random = Math.floor(Math.random() * (quantityOfElements - 1)); delete 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
259.5 Ops/sec
Object
264.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and understand what is being tested. **Benchmark Definition:** The benchmark definition script creates three data structures: 1. An array `array` with 1,000,000 elements, each containing an object with a unique `id` property. 2. A `Map` instance `map`, which stores the objects from the array using their `id` properties as keys. 3. An empty object `obj`. The script then populates `map` and `obj` by iterating over the elements of `array`. This is done to ensure that both data structures contain the same elements. **Options Compared:** Two options are being compared: 1. **Map**: The `Map` instance is used as a key-value store, where each object in the array is associated with its corresponding `id`. 2. **Object**: An empty object (`obj`) is used to store the objects from the array using their `id` properties as keys. **Pros and Cons:** * **Map**: * Pros: * Fast lookups, insertions, and deletions due to its hash-based indexing. * Efficient storage for large datasets. * Cons: * May have slower initial population compared to `Object` since it requires creating a hash table. * **Object**: * Pros: * Fast lookups can be achieved using bracket notation (`obj[random]`), similar to `Map`. * No overhead of creating a hash table, making it faster for the first iteration. * Cons: * Slower lookups and deletions compared to `Map` due to object property lookup. **Library:** * **Array**: The built-in JavaScript `Array` type is used as the underlying data structure for storing elements in the benchmark. * **Map**: The built-in JavaScript `Map` type is used as the key-value store for fast lookups and insertions. **Special JS Features or Syntax:** None are explicitly mentioned in the provided code. However, it's worth noting that: * Bracket notation (`obj[random]`) is used to access properties of an object, which can be faster than dot notation in some cases. * The `let` keyword with block scope is used for variable declarations, ensuring that variables are scoped correctly and preventing global variable pollution. **Other Alternatives:** * **Set**: Instead of using a `Map`, you could use a `Set` to store unique elements. However, this would not preserve the original order of elements. * **WeakMap**: If you need to support sparse data structures or allow for garbage collection of key-value pairs, consider using a `WeakMap`. * **Other data structures**: Depending on your specific requirements, other data structures like trees (e.g., binary search trees), graphs, or even arrays with additional indexing might be more suitable. When selecting an alternative data structure, consider the trade-offs between performance, memory usage, and complexity: * For fast lookups and insertions, `Map` might still be a good choice. * If you need to prioritize initial population speed over subsequent lookups, `Object` could be a better option. * When dealing with sparse data or requiring garbage collection support, consider using a `WeakMap`. * For complex, tree-like structures or graph data, other data structures might provide better performance and scalability.
Related benchmarks:
map vs forEach Chris
map vs forEach Chris v2b
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 set array to map
Comments
Confirm delete:
Do you really want to delete benchmark?