Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
map vs object - key access 2
(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(100000), (_, x) => ({ key: x, value: x * 10 })); var objContainer = {}; var mapContainer = new Map(); for (let i = 100; 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:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2.1 Safari/605.1.15
Browser/OS:
Safari 17 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Object access
187.4 Ops/sec
Map get
200.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, along with the pros and cons of different approaches. **Benchmark Overview** The benchmark measures the performance difference between accessing data using an object (specifically, `objContainer`) versus using a Map (`mapContainer`). The test case creates two containers: one for objects (`objContainer`) and another for Maps (`mapContainer`). It then populates both with items from an array of objects. **Benchmark Definition JSON** The benchmark definition provides two scripts that will be executed: 1. `items.forEach(item => objContainer[item.value])`: This script iterates over the array of objects, accessing each object's value and using it as a key to access the corresponding value in the `objContainer` object. 2. `items.forEach(item => mapContainer.get(item.value))`: This script does the same thing as the previous one, but uses a Map (`mapContainer`) to store and retrieve values. **Options Compared** The two scripts are comparing the performance of accessing data using: * An object (`objContainer`) * A Map (`mapContainer`) **Pros and Cons of Each Approach** 1. **Object Access (objContainer)**: * Pros: Simple, lightweight, and easy to implement. * Cons: May not be as efficient as Maps for large datasets or complex lookups, since objects are stored in a linear array. 2. **Map Access (mapContainer)**: * Pros: Efficient for large datasets or complex lookups, since Maps provide fast lookup times using the `get()` method. * Cons: Requires additional memory and processing power to create and maintain the Map. **Library Usage** The benchmark uses the following libraries: 1. None explicitly mentioned in the JSON definition; however, it's likely that the JavaScript engine being tested (e.g., V8) provides internal implementations for objects and Maps. 2. `Array.from()` is used to create an array of items from an array of objects. **Special JS Features or Syntax** None mentioned in this benchmark. **Other Considerations** When choosing between object access and Map access, consider the following: * **Performance**: If you need fast lookup times for large datasets, use a Map. For simple lookups with small datasets, an object might be sufficient. * **Memory Usage**: Maps require more memory than objects, especially when dealing with large datasets. **Alternatives** Other alternatives to compare in benchmarking JavaScript performance include: 1. Using an array of primitives (e.g., numbers) instead of objects or Maps. 2. Adding additional processing steps between access and retrieval (e.g., calculations or sorting). 3. Comparing the performance of different data structures, such as sets, queues, or trees. Keep in mind that benchmarking JavaScript performance requires careful consideration of various factors, including hardware, engine version, and testing methodology.
Related benchmarks:
Object keys vs Array map v2
map vs object - key access
map vs object - key access 4
Object key access vs array find vs map
Comments
Confirm delete:
Do you really want to delete benchmark?