Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Object vs Map 5
(version: 0)
Comparing performance of:
Map vs Obj
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const rnd = () => Math.random().toString(36); const base = Array.from({length: 200}, () => [rnd(), rnd()]); const rndKey = () => base[Math.floor(Math.random() * base.length)][0]; const map = new Map(base); const obj = Object.setPrototypeOf(Object.fromEntries(base), null); 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:
Map
testMap()
Obj
testObj()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Map
Obj
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:140.0) Gecko/20100101 Firefox/140.0
Browser/OS:
Firefox 140 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Map
72482.1 Ops/sec
Obj
46053.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **Benchmark Definition** The benchmark is designed to compare the performance of two data structures: `Object` and `Map`. The test creates an array of 200 objects, where each object has two properties (key-value pairs). The test then uses these arrays to create a `Map` and an `Object`, respectively. The `Map` is initialized using the `Array.from()` method and its key-value pairs are used as keys and values, respectively. The test case consists of three main steps: 1. Initialize the map and object with random key-value pairs. 2. Perform some work on the data structures (e.g., adding new entries, deleting existing ones). 3. Measure the time taken to execute a function that pushes the results to an array (`acc`). **Options Compared** Two options are compared in this benchmark: 1. **Map**: The test creates a `Map` instance using the `Array.from()` method and its key-value pairs as keys and values, respectively. 2. **Object**: The test creates an `Object` instance using the `Object.fromEntries()` method and its key-value pairs as properties. **Pros and Cons** Here are some pros and cons of each approach: * **Map**: + Pros: Maps are designed for key-value pairs and provide efficient lookups, insertions, and deletions. They are also more memory-efficient than objects. + Cons: Maps have additional overhead due to the need to store keys and their associated values. * **Object**: + Pros: Objects are more flexible and can be used for a wider range of data structures, such as complex nested objects. + Cons: Object lookups, insertions, and deletions can be slower than those in maps. **Other Considerations** In this benchmark, the test disables optimizations using `eval()` to ensure that the results are not influenced by the JavaScript engine's optimization techniques. Additionally, the test uses a large array of 200 objects to simulate a large dataset. **Libraries and Special JS Features** There is no specific library used in this benchmark. However, it does use some special JavaScript features: * `Array.from()` method: Creates an array from an iterable object. * `Object.fromEntries()` method: Creates an object from key-value pairs. * `eval()` function: Evaluates a string as JavaScript code. **Alternative Approaches** Other alternatives to compare the performance of data structures include: 1. Using other data structures, such as arrays or sets. 2. Adding more complex operations (e.g., sorting, searching) to the test cases. 3. Using different algorithms for data storage and retrieval. 4. Measuring performance under different workload patterns. It's worth noting that MeasureThat.net provides a wide range of benchmark templates and examples, allowing users to create and customize their own benchmarks for various JavaScript features and use cases.
Related benchmarks:
Large Map vs Object 2
Map vs Array vs Object set uint32 key speed2
Map vs Array vs Object vs Set add item speed in 50000 iters 2
Map vs Array vs Set vs Object set uint32 key speed fork
Comments
Confirm delete:
Do you really want to delete benchmark?