Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object vs Map 5.2
(version: 0)
Comparing performance of:
testMap vs testObj
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function fromEntriesN(entries){ var obj = Object.create(null); for(var i = 0; i < entries.length; i++) { obj[entries[i][0]] = entries[i][1]; } return obj; }; function createBase(n) { var base = new Array(n); while(n--) { base[n] = [rnd(), rnd()]; } return base; } const rnd = function(){return Math.random().toString(36)}; const base = createBase(200); const rndKey = function(){return base[Math.floor(Math.random() * base.length)][0]}; const map = new Map(base); const obj = fromEntriesN(base); const acc = []; function testMap() { const newKey = rnd(); map.set(newKey, rnd()); acc.push( map.get(rnd()), map.get(rndKey()) ); // doing actual work map.set(rndKey(), rnd()); map.delete(newKey); eval('acc.push("' + rnd() + '");'); // disable optimisations acc.length = 0; } function testObj() { const newKey = rnd(); obj[newKey] = rnd(); acc.push( obj[rnd()], obj[rndKey()] ) // doing actual work obj[rndKey()] = rnd(); delete obj[newKey]; eval('acc.push("' + rnd() + '");'); // disable optimisations acc.length = 0; // doing actual work }
Tests:
testMap
testMap()
testObj
testObj()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
testMap
testObj
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):
**Overview** The provided JSON represents a JavaScript microbenchmark test case for measuring the performance of two different approaches: using a `Map` data structure and using an object (`Object`) to store key-value pairs. The benchmark aims to compare these two approaches, which are often used in JavaScript programming. **Options compared** Two options are compared: 1. **Map**: A built-in JavaScript data structure that stores key-value pairs in a hash table. 2. **Object**: A fundamental data type in JavaScript that can be used to store key-value pairs. **Pros and Cons of each approach** **Map:** Pros: * Fast lookups and insertions, with an average time complexity of O(1). * Efficient use of memory, as it stores only the keys and values that are actually accessed. Cons: * Can be slower than objects for certain operations (e.g., iterating over all key-value pairs) due to the overhead of creating a hash table. * May have additional overhead for large datasets, depending on the implementation. **Object:** Pros: * Familiar syntax and semantics for most developers. * Can be more flexible than maps for certain use cases, such as using methods like `hasOwnProperty()` or `in`. Cons: * Slower lookups and insertions compared to maps, with an average time complexity of O(n), where n is the number of key-value pairs. * More memory-intensive than maps, as it stores all key-value pairs in memory. **Library used** In this benchmark, no external libraries are required. However, the `Object.create(null)` method is used to create a prototype chain for objects that doesn't inherit from any built-in object. **Special JS feature or syntax** The `eval()` function is used in some test cases to disable optimizations and ensure that the compiler can't optimize away the benchmarked code. This allows the test case to measure the performance of the actual execution time, rather than any potential optimizations. **Other alternatives** There are other data structures that could be used instead of maps or objects, such as: * **Arrays**: While not ideal for key-value pairs, arrays can still be used with some creativity (e.g., using an object to index into the array). * **Sets**: A built-in JavaScript data structure that stores unique values in a hash table. * **WeakMaps**: A specialized map implementation that only tracks weak references to objects. Keep in mind that each of these alternatives would have its own trade-offs and performance characteristics.
Related benchmarks:
Large Map vs Object 2
Object vs Map 5
Map vs Object 5000 rand
Array.find vs. Map.get 20241610 2
Comments
Confirm delete:
Do you really want to delete benchmark?