Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map vs Object (real-world) Performance (string keys)
(version: 0)
Comparing performance of:
Conspicuous Map lookup vs Conspicuous Obj lookup
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var keyCount = 16384 var keys = [] var map = new Map() var obj = {} // Hide lookup keys to prevent V8 cheating (AKA Optimizing) var getConspicuousKey = seed => keys['___' + Math.floor(seed * keyCount)] // Setup out test objects w/ random values for (let i=0; i<keyCount; i++) { let val = Math.random() let key = Math.random() keys.push(key) map.set(key,val) obj[key] = val }
Tests:
Conspicuous Map lookup
for (let i=0; i<keyCount; i++) { let seed = Math.random() let key = getConspicuousKey(seed) a = map.get(key) }
Conspicuous Obj lookup
for (let i=0; i<keyCount; i++) { let seed = Math.random() let key = getConspicuousKey(seed) a = obj[key] }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Conspicuous Map lookup
Conspicuous Obj lookup
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Conspicuous Map lookup
39.1 Ops/sec
Conspicuous Obj lookup
40.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its test cases. **Benchmark Overview** The benchmark compares the performance of two data structures in JavaScript: `Map` and `Object`. Both data structures are used to store key-value pairs, but they have different underlying implementation details. The benchmark aims to measure which one is faster for real-world use cases. **Options Compared** Two main options are compared: 1. **Conspicuous Map lookup**: This involves looking up a value in the `Map` using a random key. The `getConspicuousKey` function generates a unique and seemingly random key by appending a string to an array of keys. 2. **Conspicuous Object lookup**: Similar to the map lookup, but uses the `Object` data structure to look up a value. **Pros and Cons** Both options have their pros and cons: * **Conspicuous Map lookup** + Pros: - The `Map` data structure is optimized for key-based lookups, which can be faster in some cases. + Cons: - The use of a seemingly random key might not accurately represent real-world usage patterns. - The `getConspicuousKey` function adds overhead due to the string concatenation. * **Conspicuous Object lookup** + Pros: - The `Object` data structure is more familiar and easier to work with for many developers. + Cons: - As mentioned earlier, key-based lookups might not be as efficient as those in `Map`. **Library/Functionality** * **getConspicuousKey**: This function generates a unique and seemingly random key by appending a string to an array of keys. Its purpose is to create a test case that simulates real-world usage patterns, where the key is not necessarily contiguous or predictable. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in this benchmark that require specific knowledge of the language. **Other Alternatives** If you're interested in exploring alternative data structures or optimization techniques, here are a few options: * **Set**: A `Set` is another data structure that can be used for fast key-based lookups. However, it's not as flexible as an `Object`. * **WeakMap**: A `WeakMap` is similar to a `Map`, but it only stores weak references to keys. This can make it more efficient in certain situations. * **Optimization techniques**: Depending on the specific use case, optimization techniques like caching, memoization, or code splitting might be worth exploring. Keep in mind that these alternatives may not directly impact the performance of the `Map` and `Object` data structures, but they can influence overall application performance.
Related benchmarks:
Map has vs get
Array.forEach vs Object.keys().forEach
entries vs keys lookup
Object.entries VS Object.keys with obj[key]
Object.fromEntries vs Map
Comments
Confirm delete:
Do you really want to delete benchmark?