Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map vs Object Access Speed
(version: 0)
Lookup of map vs object
Comparing performance of:
Map lookup vs Obj lookup
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var map = new Map(); var obj = {}; let entry_num = 1000000; let round_num = entry_num * 100; for (i = 0; i < entry_num; i++) { let a = (Math.random() * round_num) % round_num; a = map.set("" + a, {}); obj["" + a] = {}; } map.set('a', 5); obj['a'] = 5; var i = 0, count = 1000, a;
Tests:
Map lookup
for (i = 0; i < count; i++) { a = map.get('' + i); }
Obj lookup
for (i = 0; i < count; i++) { a = obj['' + i]; }
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:
2 years ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 17_3_1 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.3.1 Mobile/15E148 Safari/604.1
Browser/OS:
Mobile Safari 17 on iOS 17.3.1
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Map lookup
7069.9 Ops/sec
Obj lookup
6550.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Definition** The benchmark is defined by two JSON objects: 1. **Script Preparation Code**: This code creates a new `Map` object (`map`) and an empty object (`obj`). It then populates both data structures with 1 million entries, where each entry is a random number between 0 and 100,000. The numbers are used as keys in the map and object, respectively. 2. **Html Preparation Code**: This field is empty, which means that no HTML code is provided for preparation. **Individual Test Cases** The benchmark consists of two test cases: 1. **Map Lookup**: This test case uses the `get()` method to retrieve values from the `map` object using a random key generated by `(Math.random() * round_num) % round_num`. The test case runs 1000 iterations. 2. **Object Lookup**: This test case uses bracket notation (`obj['' + i]`) to access values in the `obj` object using a similar approach as above. **Options Compared** The benchmark is comparing two approaches: 1. **Map Lookup**: Using the `get()` method on the `map` object. 2. **Object Lookup**: Using bracket notation (`obj['' + i]`) on the `obj` object. **Pros and Cons of Each Approach** * **Map Lookup (get() method)**: + Pros: - Can be more efficient for large datasets, as it uses a hash table internally. - May be faster due to the optimized implementation in modern JavaScript engines. + Cons: - Can be slower for small datasets or when using `get()` on an empty map. * **Object Lookup (bracket notation)**: + Pros: - Generally faster for small datasets, as it uses a simple lookup table. - May be more readable and intuitive for some developers. + Cons: - Can be slower for large datasets or when using bracket notation on an empty object. **Library Usage** Neither the `Map` class nor the `Object` prototype is explicitly mentioned in the benchmark definition. However, it's likely that modern JavaScript engines (like those used by Mobile Safari 17) have optimized implementations of these built-in objects. **Special JS Features or Syntax** There are no special features or syntax being tested in this benchmark. The code is using standard JavaScript syntax and features. **Other Alternatives** If you wanted to rewrite the benchmark with alternative approaches, some options could be: 1. Using `Array.prototype.indexOf()` instead of bracket notation for object lookup. 2. Using a library like Lodash or Ramda to implement more complex data structures and operations (not used in this benchmark). 3. Comparing other data structures, such as arrays or Sets, with Maps and objects. Keep in mind that rewriting the benchmark may require significant changes to the code and might not accurately represent the performance differences between different approaches.
Related benchmarks:
Map vs Array vs Object vs Set add item speed in 50000 iters 2
Map vs Object read performance for a 1000 key lookup
Object vs Map lookup w/ rando integer keys
Object vs Map lookup w/ rando integer key and array
Comments
Confirm delete:
Do you really want to delete benchmark?