Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Dict vs. Map vs. Array
(version: 0)
Large dictionary - how worse is it from array with direct reference? And how does Map perform?
Comparing performance of:
Dictionary vs Array vs Map
Created:
7 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var dict= {} for (var i=0; i<50000; i++) { dict['abc_'+i] = i; } var map= new Map() for (var i=0; i<50000; i++) { map.set('abc_'+i, i); } var arr = [] for (var i=0; i<50000; i++) { arr.push(i); }
Tests:
Dictionary
let c = 0 for (i=2000; i<10000; i+=115) { c += dict['abc_'+i]}
Array
let c = 0 for (i=2000; i<10000; i+=115) { c += arr[i]}
Map
let c = 0 for (i=2000; i<10000; i+=115) { c += map.get('abc_'+i)}
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Dictionary
Array
Map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36
Browser/OS:
Chrome 143 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Dictionary
251113.2 Ops/sec
Array
408423.5 Ops/sec
Map
130588.2 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. **Benchmark Overview** The benchmark is designed to compare the performance of three data structures: an array, a Map (similar to JavaScript's Object), and a dictionary (JavaScript's Object). The test cases are prepared using the provided Script Preparation Code. **Script Preparation Code** The script prepares large arrays, dictionaries, and Maps with 50,000 elements each. It then defines a function `c` that iterates over the arrays, dictionaries, or Maps to calculate their sum using various methods: * Array: accessing elements by index (`arr[i]`) * Dictionary: accessing elements by key (`dict['abc_'+i]`) * Map: accessing elements using the `get()` method (`map.get('abc_'+i)`) **Options Compared** The benchmark compares the following options: 1. **Dictionary**: Accessing elements by key using `dict['abc_'+i]` 2. **Array**: Accessing elements by index using `arr[i]` 3. **Map**: Accessing elements using the `get()` method with a string key (`map.get('abc_'+i)`) **Pros and Cons of Each Approach** 1. **Dictionary**: * Pros: Fast lookups, since keys are hashed. * Cons: Slower iteration due to slower indexing on objects. 2. **Array**: * Pros: Simple, fast indexing. * Cons: May lead to slow iterations if the array is very large. 3. **Map**: * Pros: Efficient for large datasets with unique keys, as it uses hashing. * Cons: May have additional overhead due to the `get()` method and the need for a key string. **Library and Purpose** The script uses the following libraries: 1. None explicitly mentioned; however, JavaScript's built-in objects (Array, Object, Map) are used. **Special JS Feature or Syntax** None mentioned in this specific benchmark, as it only involves basic iteration and data access methods. **Other Alternatives** Alternatives to this benchmark could include: * Comparing other data structures like Sets or Linked Lists. * Using different iteration methods, such as using `forEach()` or `reduce()`. * Adding more complex operations, such as sorting or inserting elements. These alternatives would help further evaluate the performance characteristics of each data structure in various scenarios.
Related benchmarks:
iterating from a filled object VS iterating from a map
Array.forEach vs Object.keys().forEach
Array.from() vs new Array().map()
new Map vs Array.from vs spread operator
new Map vs set array to map
Comments
Confirm delete:
Do you really want to delete benchmark?