Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map vs Object Longer
(version: 0)
Lookup of map vs object
Comparing performance of:
Map lookup vs Obj lookup
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var map = new Map(); var obj = {}; map.set('a', 5); map.set('b', 5); map.set('c', 5); map.set('d', 5); map.set('e', 5); obj['a'] = 5; obj['b'] = 5; obj['c'] = 5; obj['d'] = 5; obj['e'] = 5; var i = 0, count = 1000, a;
Tests:
Map lookup
for (i = 0; i < count; i++) { a = map.get('a'); a = map.get('b'); a = map.get('c'); a = map.get('d'); a = map.get('e'); }
Obj lookup
for (i = 0; i < count; i++) { a = obj['a']; a = obj['b']; a = obj['c']; a = obj['d']; a = obj['e']; }
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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested, compared options, pros and cons of those approaches, and other considerations. **Benchmark Overview** The provided benchmark compares the performance of two data structures in JavaScript: Maps and objects. Both data structures are used to store key-value pairs, but they differ in how keys are stored and accessed. **Script Preparation Code** The script preparation code creates an instance of a Map (`map`) and an object (`obj`), both initialized with the same key-value pairs: ```javascript var map = new Map(); var obj = {}; map.set('a', 5); map.set('b', 5); map.set('c', 5); map.set('d', 5); map.set('e', 5); obj['a'] = 5; obj['b'] = 5; obj['c'] = 5; obj['d'] = 5; obj['e'] = 5; ``` **Html Preparation Code** There is no HTML preparation code provided, which means the benchmark only focuses on JavaScript performance. **Individual Test Cases** The benchmark consists of two individual test cases: 1. **Map Lookup**: This test case iterates `count` times, accessing each key in the map using the `get()` method: ```javascript for (i = 0; i < count; i++) { a = map.get('a'); a = map.get('b'); a = map.get('c'); a = map.get('d'); a = map.get('e'); } ``` 2. **Object Lookup**: This test case is similar to the previous one, but it accesses keys in the object using bracket notation (`[]`): ```javascript for (i = 0; i < count; i++) { a = obj['a']; a = obj['b']; a = obj['c']; a = obj['d']; a = obj['e']; } ``` **Comparison** The benchmark compares the performance of these two test cases, which essentially boil down to: * Accessing keys in a Map using `get()`. * Accessing keys in an object using bracket notation (`[]`). **Pros and Cons of Each Approach** 1. **Map Lookup (get())** * Pros: + More efficient for large datasets since maps use a hash table under the hood. + Supports fast lookup, insertion, and deletion operations. * Cons: + May require more memory since each key-value pair is stored as a separate entity in the map. 2. **Object Lookup ([])** * Pros: + More lightweight since objects use a hash table-like structure internally for keys. + Supports fast lookup, insertion, and deletion operations. * Cons: + May not be as efficient for very large datasets since each key-value pair is stored sequentially. **Library Used** No specific JavaScript library is used in this benchmark. However, both `Map` and objects use built-in JavaScript data structures and methods. **Special JS Features or Syntax** There are no special JavaScript features or syntax used in this benchmark. The code uses standard JavaScript data types (Maps, objects) and basic iteration constructs (`for` loops). **Other Considerations** * The benchmark only focuses on performance, not security, readability, or maintainability. * The `count` variable determines the number of iterations performed by each test case. * The `executionsPerSecond` metric provides a measure of each test case's performance in terms of the number of executions per second. **Alternatives** Other data structures and libraries that can be used for similar benchmarking purposes include: * Arrays * Sets * WeakMaps (for garbage collection considerations) * Libraries like Lodash or Ramda, which provide alternative data structure implementations or utility functions.
Related benchmarks:
Array from() vs Map.keys()
Array from() vs Map.keys() vs Map.values() vs spread
array includes vs object key lookup, large arrays
Map vs Object read performance for a 1000 key lookup
Array vs Class
Comments
Confirm delete:
Do you really want to delete benchmark?