Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map vs Object 1
(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 = {}; map.set('a', 5); obj['a'] = 5; var i = 0, count = 100000, a;
Tests:
Map lookup
for (i = 0; i < count; i++) { a = map.get('a'); }
Obj lookup
for (i = 0; i < count; i++) { a = obj['a']; }
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):
**Benchmark Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks to compare the performance of different approaches. The provided benchmark measures the execution time of two types of lookups: Map lookup and Object lookup. **Tested Options** The benchmark compares two options: 1. **Map Lookup**: This option uses the `Map` data structure in JavaScript, which is a built-in object that stores key-value pairs. 2. **Object Lookup**: This option uses the built-in object literal syntax to create an object and store a value under a key. **Pros and Cons** * **Map Lookup**: + Pros: Maps are designed for fast lookups, with an average time complexity of O(1). + Cons: Maps require more memory than objects, as they need to store the underlying array. * **Object Lookup**: + Pros: Objects are lightweight and don't require extra memory. + Cons: Object lookup has a time complexity of O(n), where n is the number of properties in the object. This can be slower for large datasets. **Library Usage** The benchmark uses the `Map` class from JavaScript's built-in API, which is part of the ECMAScript standard. The purpose of this library is to provide a fast and efficient way to store key-value pairs. **Special JS Feature/Syntax** This benchmark does not use any special JavaScript features or syntax, making it accessible to a wide range of developers. **Other Alternatives** If you're looking for alternative data structures for lookups, you could consider: 1. **Arrays**: While arrays are not optimized for lookups, they can be used as an alternative. However, this would likely result in slower performance compared to Maps or objects. 2. **Data Structures like `WeakMap`**: WeakMaps are another type of Map implementation that uses a weak reference to store key-value pairs, making it useful for scenarios where the map is not needed to prevent memory leaks. **Benchmark Preparation Code** The provided benchmark preparation code creates an empty Map and object, sets a value under a key in each, and then declares a variable `a` that will be used in the test cases. The HTML preparation code is empty, indicating that no additional setup is required for the benchmark. **Individual Test Cases** The benchmark consists of two individual test cases: 1. **Map Lookup**: This test case iterates over a loop 100,000 times and retrieves the value associated with the key `'a'` using `map.get('a')`. 2. **Object Lookup**: This test case iterates over a loop 100,000 times and retrieves the value associated with the key `'a'` using `obj['a']`. The results of each test case are stored in the `ExecutionsPerSecond` field of the benchmark result, which provides an estimate of how many executions per second can be achieved for each option.
Related benchmarks:
Array from() vs Map.keys()
Array from() vs Map.keys() vs Map.values() vs spread
Array from() vs Map.keys() vs Map.values() vs spread (fixed)
array includes vs object key lookup, large arrays
Map vs Object read performance for a 1000 key lookup
Comments
Confirm delete:
Do you really want to delete benchmark?