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(i+","+j)) results.push(map.get(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:
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 test cases. **Benchmark Definition JSON** The benchmark is designed to compare two approaches: using a `Map` data structure for key-value storage versus accessing an array with indexed lookup. Here's what's being tested: * A map (`map`) is created, which stores key-value pairs. * An array (`arrLookup`) is also created, which will be used as an alternative data structure for key-value storage. * A large dataset of `items` (arrays) is generated, with each item containing another array of 100 elements. * For each element in the `items` array, there's a 10% chance that the corresponding value in the map or arrLookup will be set. The value is randomly generated and has a 50% chance of being a random number. **Test Cases** There are two individual test cases: 1. **Map lookup**: This test case uses the `map` data structure to access its values. * The benchmark definition script initializes an empty array `results` to store the retrieved values. * It then iterates through each element in the `items` array and checks if a value exists in the map using the `has()` method. If it does, the corresponding value is pushed onto the `results` array using the `get()` method. 2. **Array lookup**: This test case uses the `arrLookup` array data structure to access its values. * The benchmark definition script initializes an empty array `results` to store the retrieved values. * It then iterates through each element in the `items` array and checks if the corresponding value exists in arrLookup using a conditional statement (`typeof arrLookup[i][j] !== "undefined"`). If it does, the value is pushed onto the `results` array. **Options Compared** The two test cases are comparing the performance of: 1. Using a `Map` data structure for key-value storage (Map lookup) 2. Using an array with indexed lookup for key-value storage (Array lookup) **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **Map lookup** + Pros: - Faster lookups due to the hash-based search algorithm used by `Map`. - Efficient handling of large datasets. + Cons: - Requires more memory overhead compared to arrLookup, as it stores additional metadata (key-value pairs). * **Array lookup** + Pros: - Less memory overhead compared to Map, as only the indexed values are stored. - Simpler implementation and less dependency on JavaScript's built-in `Map` data structure. + Cons: - Slower lookups due to the linear search algorithm used in array iteration. **Other Considerations** * **Library usage**: The benchmark uses the built-in `Map` data structure provided by JavaScript. No additional libraries are required for this benchmark. * **Special JS feature/syntax**: There is no special JavaScript feature or syntax being tested in this benchmark. It's a straightforward comparison of two simple data structures. **Alternatives** If you want to explore alternative approaches, consider the following: 1. **Object-oriented lookup**: Instead of using arrays or maps, you could use objects with named properties for key-value storage. 2. **Hash tables**: Implement a custom hash table algorithm and compare its performance to the built-in `Map` data structure. 3. **Bloom filters**: Use Bloom filters as an alternative to `Map` for fast lookups, but at the cost of higher memory usage. Keep in mind that these alternatives might require significant modifications to your benchmark code.
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?