Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Read Map vs Object
(version: 0)
Comparing performance of:
Obj / Object.entries() vs Map / spread
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Obj / Object.entries()
let obj = {}; let map = new Map(); for (let i = 0; i < 10000; i++) { let key = `${i}`.padStart(10, '0'); let val = Math.random(); map.set(key, { key, val }); obj[key] = { key, val }; } for (let i = 0; i < 10000; i++) { let k = Math.floor(10000 * Math.random()); let key = `${k}`.padStart(10, '0'); let val = map.get(key); }
Map / spread
let obj = {}; let map = new Map(); for (let i = 0; i < 10000; i++) { let key = `${i}`.padStart(10, '0'); let val = Math.random(); map.set(key, { key, val }); obj[key] = { key, val }; } for (let i = 0; i < 10000; i++) { let k = Math.floor(10000 * Math.random()); let key = `${k}`.padStart(10, '0'); let val = obj[key]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Obj / Object.entries()
Map / spread
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Linux; arm_64; Android 13; ASUS_AI2202) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.5993.107 YaBrowser/23.11.3.107.00 SA/3 Mobile Safari/537.36
Browser/OS:
Yandex Browser 23 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Obj / Object.entries()
117.8 Ops/sec
Map / spread
101.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its individual test cases. **Benchmark Definition JSON** The provided benchmark definition is empty, which means that it doesn't specify any particular JavaScript version or syntax features to be tested. This allows the users to create their own benchmarks using various options. **Individual Test Cases** There are two test cases: 1. **Test Case 1: "Read Map vs Object"** This test case compares the performance of reading data from a `Map` object versus an object literal (`obj`) when accessing values by key. The script preparation code creates a large map and an empty object, then populates both with key-value pairs using a loop. The test case then runs two iterations: a. For each iteration, it randomly selects a key and value from the `Map` object and accesses the corresponding value. b. For each iteration, it randomly selects a key and value from the object literal (`obj`) and accesses the corresponding value. The comparison is done using two approaches: * **Option 1: Map / spread**: This option uses the spread operator (`...`) to convert the `Map` object into an array of key-value pairs, which can then be iterated over. The test case then uses this array to access values. * **Option 2: Object.entries()**: This option uses the `Object.entries()` method to get an array of key-value pairs from the object literal (`obj`). The test case then uses this array to access values. **Pros and Cons** **Map / spread (Option 1)** Pros: * Can be more efficient for large datasets, as it allows for direct iteration over the `Map` object. * Can be more readable, as it clearly separates key-value pairs from the underlying data structure. Cons: * May not work correctly if the `Map` object is very large or has a lot of duplicate keys (as this can lead to unnecessary computations). * Requires the spread operator (`...`) to convert the map into an array. **Object.entries() (Option 2)** Pros: * Works well for both small and large datasets. * Is more concise and easy to read, as it directly accesses key-value pairs without needing additional computations. Cons: * May be slower than `Map / spread` due to the extra overhead of calling `Object.entries()`. **Library/Functionality** None of the test cases rely on any external libraries or functions other than built-in JavaScript features. **Special JS Feature/Syntax** The test case uses the following special syntax features: * **Spread operator (`...`)**: used in Option 1 to convert the `Map` object into an array. * **Object.entries() method**: used in Option 2 to get an array of key-value pairs from the object literal (`obj`). * **Random number generation using Math.random()**: used throughout both test cases. **Other Alternatives** If you want to explore alternative approaches, consider the following: * Using `for...of` loop instead of `Object.entries()` for iterating over key-value pairs. * Using a custom implementation for accessing values in the map and object literals, such as using a simple function with `Object.keys()` or `Map.forEach()`. * Adding additional test cases to compare performance across different data structures (e.g., arrays, sets) or access patterns (e.g., random vs sequential).
Related benchmarks:
Array vs Object vs Map vs WeakMap access2
Array vs Object vs Map vs WeakMap access (1)
Map has vs get
Array vs Object vs Map vs WeakMap access (3) vs Object key
Object.fromEntries vs Map
Comments
Confirm delete:
Do you really want to delete benchmark?