Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map vs Object 2
(version: 0)
Set 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 = {}; map.set('a', 5); obj['a'] = 5; var i = 0, count = 1000, a;
Tests:
Map lookup
for (i = 0; i < count; i++) { map.set('a',6); }
Obj lookup
for (i = 0; i < count; i++) { obj['a'] = 6; }
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/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Map lookup
24893.9 Ops/sec
Obj lookup
30766.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark definition and test cases for you. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmarking test case created on MeasureThat.net. The test case is designed to compare the performance of two data structures in JavaScript: `Map` and objects (specifically, arrays). **Script Preparation Code** The script preparation code initializes two variables: * `map`: a new instance of the `Map` data structure. * `obj`: an empty object (in JavaScript, objects are essentially arrays with string keys and values). * Two lines set the value for key `'a'` in both `map` and `obj`, respectively. **Html Preparation Code** The HTML preparation code is null, which means that no HTML page or template is being used to run this benchmark. The test case only executes JavaScript code. **Individual Test Cases** There are two test cases: 1. **Map lookup** * Benchmark definition: `for (i = 0; i < count; i++) { map.set('a',6); }` * This test case iterates `count` times, setting the value of key `'a'` to `6` in a new instance of `Map`. The goal is to measure the time it takes to perform this operation. 2. **Obj lookup** * Benchmark definition: `for (i = 0; i < count; i++) { obj['a'] = 6; }` * This test case iterates `count` times, setting the value of key `'a'` to `6` in an empty object (`obj`). The goal is to measure the time it takes to perform this operation. **Library** The `Map` data structure used in this benchmark is a built-in JavaScript feature. It's a collection of key-value pairs that allows for fast lookups and efficient insertion/deletion operations. **Special JS Features/Syntax** There are no specific JavaScript features or syntax used in this benchmark, aside from the use of template literals (`var a = "";`) which is not unique to JavaScript but rather a feature of modern JavaScript. **Pros and Cons of Different Approaches** Here's a brief analysis of the two approaches: 1. **Map lookup** * Pros: `Map` provides fast lookups due to its hash-based implementation, making this approach suitable for large datasets. * Cons: Creating a new instance of `Map` on each iteration can be expensive in terms of memory allocation and garbage collection. 2. **Obj lookup** * Pros: Objects are more widely supported across browsers and platforms, making this approach more portable. * Cons: Object lookups can be slower due to the need for dynamic property lookup, which involves traversing the object's prototype chain. **Other Considerations** When running this benchmark, it's essential to consider the following: * The size of the `count` variable will impact the performance difference between the two approaches. * The specific JavaScript engine and browser being used can affect the performance results. * The test case doesn't account for potential caching or optimization techniques that might be used by the browser or JavaScript engine. **Alternatives** If you'd like to create similar benchmarks, consider exploring other data structures in JavaScript, such as: * Arrays * Sets * WeakMaps * Proxy objects You can also experiment with different iteration patterns, caching mechanisms, and optimization techniques to further explore the performance characteristics of each data structure.
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)
Map vs Object read performance for a 1000 key lookup
Object.fromEntries vs Map
Comments
Confirm delete:
Do you really want to delete benchmark?