Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map vs Array.
(version: 0)
Comparing performance of:
Map vs Array
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
let map = new Map(); let arr = []; let obj = {}; const randomU32 = function() { return Math.random() * (1 << 31) >>> 0; }
Tests:
Map
const randomU32 = function() { return Math.random() * (1 << 31) >>> 0; } const map = new Map(); map.set(randomU32(), true);
Array
const randomU32 = function() { return Math.random() * (1 << 31) >>> 0; } const arr = []; arr[randomU32()] = true;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Map
Array
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its different test cases. **Benchmark Overview** The benchmark compares two approaches: using a `Map` data structure versus an array to store key-value pairs with unique keys. The goal is to measure which approach is faster for this specific use case. **Options Compared** 1. **Map**: A built-in JavaScript object that stores key-value pairs in an ordered sequence. Maps are implemented as hash tables, making lookups and insertions relatively efficient. 2. **Array**: A built-in JavaScript data structure that stores a collection of values in a contiguous block of memory. Arrays can be used to store key-value pairs using indexing or properties. **Pros and Cons** 1. **Map**: * Pros: Efficient lookups, insertions, and deletions (average case O(1)). * Cons: Key collisions can lead to slower performance in rare cases. 2. **Array**: * Pros: Simple to implement, easy to understand, and often faster for small datasets. * Cons: Slower lookups and insertions compared to Map (O(n) on average). **Library Usage** None of the test cases use any external libraries. **Special JS Features/Syntax** There are no special JavaScript features or syntax used in these test cases. They only rely on built-in JavaScript constructs. **Benchmark Preparation Code** The script preparation code initializes: * A new `Map` instance (`let map = new Map();`) * An empty array (`let arr = [];`) * An empty object (`let obj = {};`) * A function to generate a random 32-bit unsigned integer (`const randomU32 = function() { ... }`) The generated random number is used as the key for both the `Map` and array insertions. **Test Cases** There are two test cases: 1. **Map**: Inserts a random value into the `Map` using `map.set(randomU32(), true);`. 2. **Array**: Inserts a random value into the array at an index generated by `randomU32()`, which is assigned as the key: `arr[randomU32()] = true;`. **Latest Benchmark Result** The latest benchmark results show that the Map approach outperforms the Array approach, with higher executions per second on a Mobile device running Yandex Browser 23. **Other Alternatives** If you were to implement this benchmark using other programming languages or data structures, here are some alternatives: * **Hash tables**: Similar to Maps, but implemented in languages like C++ or Java. * **Linked lists**: An alternative to arrays for storing sequences of elements. They offer faster insertions and deletions but slower traversals. * **Sets**: Data structures that only store unique values, which can be used as an alternative to both Maps and Arrays. Keep in mind that the specific results would depend on the chosen data structure, implementation, and language.
Related benchmarks:
Map vs Array vs Object set uint32 key speed
Map vs Array vs Object has uint32 key speed
Map vs Array vs Object set uint32 key speed2
Spread Operator VS Array.prototype.slice() VS Array.prototype.map()
Comments
Confirm delete:
Do you really want to delete benchmark?