Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
map vs object - key access 4
(version: 0)
Comparing performance of:
Object access vs Map get
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var items = Array.from(Array(1000), (_, x) => ({ key: x, value: x * 10 })); var objContainer = {}; var mapContainer = new Map(); for (let i = 10000; i >= 0; i--) { const index = Math.floor(Math.random() * 1000); const item = items[index]; objContainer[item.key] = item; mapContainer.set(item.key, item) }
Tests:
Object access
items.forEach(item => objContainer[item.value])
Map get
items.forEach(item => mapContainer.get(item.value))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object access
Map get
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:140.0) Gecko/20100101 Firefox/140.0
Browser/OS:
Firefox 140 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object access
130282.2 Ops/sec
Map get
74520.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll explain the benchmark and its options in detail. **Benchmark Overview** The provided JSON represents a JavaScript microbenchmark that compares two approaches: accessing an object by key using dot notation (`objContainer[item.key]`) versus accessing an object from a Map (a built-in JavaScript data structure) using the `get()` method. The benchmark is run on a large dataset of 10,000 items with random values. **Benchmark Definition** The benchmark definition consists of two test cases: 1. **Object access**: This test case uses the dot notation (`objContainer[item.value]`) to access objects in an array-like object (`objContainer`). 2. **Map get**: This test case uses the `get()` method to retrieve values from a Map (`mapContainer`). **Options Compared** The benchmark compares two options: * Accessing an object by key using dot notation (`objContainer[item.key]`) * Accessing an object from a Map using the `get()` method (`mapContainer.get(item.value)`) **Pros and Cons of Each Approach:** 1. **Object access (dot notation)**: * Pros: + Wide browser support and compatibility. + Simple to implement and understand. * Cons: + Can be slower for large datasets due to the overhead of string lookups. + May lead to more memory usage due to the creation of intermediate objects. 2. **Map get**: * Pros: + Generally faster than dot notation, especially for large datasets. + More efficient in terms of memory usage since it doesn't create intermediate objects. * Cons: + May not be supported by older browsers or those with limited JavaScript engines. **Library Used** The benchmark uses the built-in `Map` data structure and arrays (specifically, `Array.from()` and `Array`) without any external libraries. **Special JS Features/Syntax** This benchmark does not use any special JavaScript features or syntax. However, it relies on built-in browser support for `Map`, `get()`, and `Array` methods. **Other Alternatives** If the benchmark were to compare other approaches, alternatives could include: * Accessing an object using a library like Lodash (`_.at()`) or Underscore.js (`_.at_()`). * Using a different data structure, such as a WeakMap for caching. * Utilizing WebAssembly (WASM) for optimized JavaScript performance. Keep in mind that the choice of alternatives would depend on the specific requirements and constraints of the benchmark. **Benchmark Preparation Code** The provided script preparation code initializes two objects: `objContainer` and `mapContainer`, both empty, and creates an array of 10,000 items with random values. It then populates these containers with the same data using a loop.
Related benchmarks:
Object keys vs Array map v2
map vs object - key access
map vs object - key access 2
Object key access vs array find vs map
Comments
Confirm delete:
Do you really want to delete benchmark?