Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object vs Map lookup with string keys
(version: 0)
Comparing the performance of lookups in large objects vs large maps when the keys are strings.
Comparing performance of:
Map Lookup vs Object Lookup
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
window.m = new Map(); window.o = {}; for (let i = 0; i < 1000; ++i) { window.m.set('a' + i, i); window.o['a' + i] = i; }
Tests:
Map Lookup
for (let i = 0; i < 1000; ++i) { if (window.m.get('a' + i) !== i) { console.log("bad map key"); } }
Object Lookup
for (let i = 0; i < 1000; ++i) { if (window.o['a' + i] !== i) { console.log("bad object key"); } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Map Lookup
Object Lookup
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Map Lookup
5244.6 Ops/sec
Object Lookup
4305.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases to understand what's being tested. **Benchmark Definition:** The benchmark defines two tests: 1. **Map Lookup**: This test creates a large map (`window.m`) and populates it with 1000 key-value pairs using `set()`. It then iterates over the same range, checking if each value can be retrieved from its corresponding key in the map using `get()`. If any retrieval fails, an error message is logged to the console. 2. **Object Lookup**: This test creates a large object (`window.o`) and populates it with 1000 key-value pairs using string keys and values. It then iterates over the same range, checking if each value can be retrieved from its corresponding key in the object using bracket notation (`o['a+i']`). If any retrieval fails, an error message is logged to the console. **Libraries:** Both tests use built-in JavaScript objects and methods: * `Map`: A dictionary-like data structure that stores key-value pairs. * `Object`: An associative array-like data structure that stores key-value pairs using string keys. * `console.log()`, `console.error()`: The global logging functions in the browser console. **Special JS Features/Syntax:** None of these tests use special JavaScript features or syntax beyond what's standard in modern JavaScript. They rely on basic object manipulation and iteration techniques. **Options Compared:** The two tests compare the performance of: * Using a `Map` data structure to store and retrieve values. * Using an `Object` data structure with string keys to store and retrieve values. **Pros and Cons:** * **Map Lookup**: Pros: + Maps are designed for fast key-value lookups using `get()`. + They can handle large numbers of unique keys efficiently. Cons: + Maps require more memory than objects, especially for large datasets. + Some older browsers might have performance issues with maps. * **Object Lookup**: Pros: + Objects are widely supported and have good performance in most modern browsers. + They don't require as much memory as maps. Cons: + Object lookups can be slower due to the overhead of iterating over keys. + If a key is missing, it will throw an error or return `undefined`. **Considerations:** * The benchmark is comparing two common data structures in JavaScript, making it a useful test for developers who need to choose between maps and objects for their applications. * The test's focus on string keys and values makes it relevant to scenarios where key names are short and predictable. **Alternatives:** Other data structures or methods that could be used instead of `Map` or `Object` include: * Arrays with indices * Sets (for union or intersection operations) * WeakMaps (for lazy loading or caching) * Custom data structures designed for specific use cases However, these alternatives would likely require significant changes to the benchmark definition and test cases to accurately compare their performance.
Related benchmarks:
Object vs Map lookup w/ rando integer keys
Object vs Map lookup w/ rando integer key
Object vs Map lookup w/ rando integer key and array
Object vs Map lookup with int keys
Comments
Confirm delete:
Do you really want to delete benchmark?