Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map vs Object read performance for a 1000 key lookup
(version: 0)
Lookup of map vs object
Comparing performance of:
map-insert vs obj insert vs Map lookup vs Obj lookup
Created:
3 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 numberOfEntries = 100000 let i = 0; var count = 1000; var a;
Tests:
map-insert
for (let j = 0; j < numberOfEntries; j++){ const key = `a-${j}`; const value = j; map.set(key,value) obj[key] = value; }
obj insert
for (let j = 0; j < numberOfEntries; j++){ const key = `a-${j}`; const value = j; obj[key] = value; }
Map lookup
for (i = 0; i < count; i++) { const key = `a-${i}`; a = map.get(key); }
Obj lookup
for (i = 0; i < count; i++) { const key = `a-${i}`; a = obj[key]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
map-insert
obj insert
Map lookup
Obj lookup
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
5 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36 Edg/142.0.0.0
Browser/OS:
Chrome 142 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
map-insert
59.1 Ops/sec
obj insert
86.2 Ops/sec
Map lookup
5408.3 Ops/sec
Obj lookup
6516.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its results. **Benchmark Overview** The test measures the performance of JavaScript objects (specifically, arrays) versus Maps for a specific use case: frequent key-value lookups. The test consists of four individual test cases: 1. **map-insert**: Inserts 100,000 key-value pairs into a Map. 2. **obj insert**: Inserts 100,000 key-value pairs into an object (array). 3. **Map lookup**: Looks up values in the Map using the `get()` method for 1000 keys. 4. **Obj lookup**: Looks up values in the object using bracket notation (`[key]`) for 1000 keys. **Options Compared** The test compares two main options: 1. **Maps (specifically, JavaScript's built-in `Map` implementation)**: A data structure that stores key-value pairs and provides efficient lookups. 2. **Objects (specifically, arrays)**: An array of key-value pairs, which can be used for similar purposes as Maps. **Pros and Cons** **Maps:** Pros: * Efficient lookups using the `get()` method * Fast insertion of new key-value pairs Cons: * May incur a performance penalty due to object overhead (e.g., additional memory allocation) * Some browsers might have optimized Map implementations, which could lead to better performance **Objects (Arrays):** Pros: * Lightweight and efficient in terms of memory usage * Often faster for simple lookups using bracket notation (`[key]`) Cons: * Less efficient than Maps for large datasets or frequent lookups * May incur additional overhead due to JavaScript's dynamic nature **Library Usage** The test uses the built-in `Map` implementation, which is a part of JavaScript. No external libraries are required. **Special JavaScript Features/Syntax** No special JavaScript features or syntax are used in this benchmark. **Other Considerations** When choosing between Maps and objects for your use case: * If you need efficient lookups and don't mind the potential overhead, use a Map. * If you prioritize lightweight data structures and simple lookups, consider using an object (array). **Alternatives** If you're interested in exploring alternative data structures or libraries, here are some options: 1. **Native arrays**: Instead of using JavaScript's built-in `Map` implementation, you could use native arrays for your lookup data. 2. **Third-party Map libraries**: There are various third-party Map implementations available for JavaScript, such as `Lodash` or `immutable.js`. 3. **Other data structures**: You might consider using other data structures like sets, trees, or graphs, depending on your specific requirements. Keep in mind that the performance differences between these options may be negligible for small-scale applications. However, if you're working with large datasets or high-performance requirements, it's essential to choose the most suitable data structure for your use case.
Related benchmarks:
iterating from a filled object VS iterating from a map
Array.forEach vs Object.keys().forEach
array includes vs object key lookup, large arrays
Object.fromEntries vs Map
Comments
Confirm delete:
Do you really want to delete benchmark?