Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map vs Object (large number of keys)
(version: 0)
Lookup of map vs object
Comparing performance of:
Map lookup vs Obj lookup
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var map = new Map(); var obj = {}; var i = 0, count = 10000, a; for (; i < 100000; i++) { map.set('key-' + i, i); obj['key-' + i] = i; } i = 0; function getRandomKey() { return 'key-' + Math.trunc(Math.random() * 100000); }
Tests:
Map lookup
for (i = 0; i < count; i++) { a = map.get(getRandomKey()); }
Obj lookup
for (i = 0; i < count; i++) { a = obj[getRandomKey()]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Map lookup
Obj lookup
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/124.0.0.0 Safari/537.36 Edg/124.0.0.0
Browser/OS:
Chrome 124 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Map lookup
146.8 Ops/sec
Obj lookup
153.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark JSON and explain what is being tested, compared, and some pros/cons of different approaches. **Benchmark Definition** The test case compares two data structures: `Map` (JavaScript built-in object) and an equivalent object created using JavaScript's `{}` syntax. The goal is to measure which one performs better for large-scale lookup operations. The script preparation code initializes: * A `Map` instance called `map` * An empty object `obj` * A loop that sets 100,000 key-value pairs in both `map` and `obj`, using the same keys (`'key-' + i`) and values (`i`). This creates large datasets for lookup operations. The HTML preparation code is not provided, but it's likely that it initializes a random number generator to be used in subsequent lookup tests. **Individual Test Cases** Two individual test cases are defined: 1. **Map Lookup**: Iterates `count` times, retrieving the value associated with a randomly generated key using `map.get()`. 2. **Object Lookup**: Similar to the previous one, but uses direct object property access (`obj[getRandomKey()]`) instead of `map.get()`. **Comparison** The comparison is between: * `Map` (JavaScript built-in object) for lookup operations * An equivalent object created using JavaScript's `{}` syntax for lookup operations **Pros/Cons of Different Approaches** Using a `Map`: Pros: * Performance: Maps are optimized for fast lookups, as they use hash tables to store key-value pairs. * Memory efficiency: Maps can handle large amounts of data without significant memory overhead. Cons: * Syntax: The syntax is more explicit and verbose compared to object literal notation. Using an equivalent object (`{}`): Pros: * Syntax: This approach uses a more concise and readable syntax, especially for small datasets or when the number of keys is low. * Flexibility: Objects can be used as hash tables by using string properties with unique values (e.g., `'key-' + i`). Cons: * Performance: For large-scale lookup operations, objects may not perform as well as maps due to slower access times. **Library** In this test case, no external libraries are used. However, the implementation of `map.get()` relies on JavaScript's built-in `Map` object and its internal hash table data structure. **Special JS Feature or Syntax** The use of template literals (`'key-' + i`) is not specific to any particular JavaScript version or feature. Template literals were introduced in ECMAScript 2015 (ES6). Overall, this benchmark compares two common data structures used for fast lookups in JavaScript: `Map` and an equivalent object created using `{}` syntax. The results will indicate which approach performs better for large-scale lookup operations. **Other Alternatives** Alternatives to the test case could include: * Using other data structures like `Set`, `Array` or `Promise` * Implementing custom hash tables or data structures * Comparing performance with different JavaScript engines, browsers, or environments
Related benchmarks:
Long int keys (19 digits)
Object vs Map lookup w/ rando integer keys
Map vs Object (real-world) Performance (better)
Map vs Object (real-world) Performance - Forked
Map vs Object (real-world) Performance (non-numeric key)
Comments
Confirm delete:
Do you really want to delete benchmark?