Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Map lookup vs array index lookup
(version: 0)
Comparing performance of:
Map lookup vs Array lookup
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var map = new Map(); var arrLookup = []; var items = []; for (var i = 0; i < 100; i++) { items[i] = []; for (var j = 0; j < 100; j++) { items[i][j] = [i,j]; if (Math.random() < 0.1) { map.set(i+","+j, Math.random()>=0.5); if (!arrLookup[i]) arrLookup[i] = []; arrLookup[i][j] = Math.random()>=0.5; } } }
Tests:
Map lookup
var results = [], i, j; for (i = 0; i < 100; i++) { for (j = 0; j < 100; j++) { if (map.has(items[i][j] +"")) results.push(map.get(items[i][j]+"")); } } if (results.length === 0) throw Error();
Array lookup
var results = [], i, j; for (i = 0; i < 100; i++) { for (j = 0; j < 100; j++) { if (arrLookup[i] && typeof arrLookup[i][j] !== "undefined") results.push(arrLookup[i][j]); } } if (results.length === 0) throw Error();
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Map lookup
Array lookup
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
25 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36
Browser/OS:
Chrome 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Map lookup
904.0 Ops/sec
Array lookup
18487.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark. **Benchmark Definition** The website tests two different approaches to perform a look-up operation: using an array (`arrLookup`) and using a `Map` data structure (`map`). In the script preparation code, we see: * We create two arrays: `items` (which contains nested arrays) and `arrLookup`. * We populate the `items` array with 100x100 nested arrays. * For each element in the `items` array, we randomly decide whether to add an element to both `map` and `arrLookup`. If we choose to add it to `map`, we use the string representation of the key (`i+","+j`) to store a random value. **Options Compared** We have two options: 1. **Array Lookup**: We access elements in `arrLookup` using their index (`items[i][j]`). This approach is straightforward but may be slower for large arrays. 2. **Map Lookup**: We use the `has()` and `get()` methods of the `map` object to look up values by key. This approach provides faster lookup times, especially when dealing with a large number of keys. **Pros and Cons** * **Array Lookup:** * Pros: + Easy to implement and understand. + Suitable for small arrays or sparse data structures. * Cons: + May be slower due to array indexing operations. + Less flexible than `Map` lookup when dealing with large amounts of data. * **Map Lookup:** + Pros: - Faster lookup times, especially for large datasets. - More flexible and efficient way to store and retrieve key-value pairs. * Cons: + May be more complex to implement and understand. + Requires additional memory to store the `map` object. **Library/Feature** The benchmark uses JavaScript's built-in `Map` data structure. The purpose of this library is to provide a fast and efficient way to store and retrieve key-value pairs in JavaScript. **Special JS Features/Syntax** There are no special features or syntax used in this benchmark that would require additional explanation. **Alternatives** If you wanted to compare these approaches, you could also consider using other data structures like: 1. **Object**: You can use objects instead of arrays to store key-value pairs. However, accessing elements by key will be slower than array indexing. 2. **Sparse Arrays**: If your data structure is sparse (i.e., most elements are null or undefined), you might want to consider using a sparse array instead. Keep in mind that the performance differences between these approaches will depend on the specific use case and the size of the data being processed.
Related benchmarks:
array includes vs object key lookup, large arrays
new Array() vs Array.from() with random data
new Map vs Array.from vs spread operator
new Map vs set array to map
map vs reverse for vs for vs push vs unshift
Comments
Confirm delete:
Do you really want to delete benchmark?