Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test array vs map lookup
(version: 0)
Comparing performance of:
Test using Map for indexing (5000 iterations) vs Test using Array.prototype.find (5000 iterations)
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Test using Map for indexing (5000 iterations)
// Generate an array with 100 objects const rawData = []; for (let i = 0; i < 5001; i++) { rawData.push({ cost_per: Math.random() * 100, network: `network${i}`, metric: { name: `metric${i}`, description: `Description ${i}`, }, created_at: `2023-09-01T00:00:00-07:00`, }); } // Using a Map for indexing const transformedDataMap = new Map(); for (const entry of rawData) { const key = JSON.stringify({ network: entry.network, metricName: entry.metric.name, createdAt: entry.created_at, }); transformedDataMap.set(key, entry.cost_per); } const iterations = 5000; const mapLookupKey = JSON.stringify({ network: 'network50', metricName: 'metric50', createdAt: '2023-09-01T00:00:00-07:00', }); for (let i = 0; i < iterations; i++) { const mapResult = transformedDataMap.get(mapLookupKey); }
Test using Array.prototype.find (5000 iterations)
// Generate an array with 100 objects const rawData = []; for (let i = 0; i < 5001; i++) { rawData.push({ cost_per: Math.random() * 100, network: `network${i}`, metric: { name: `metric${i}`, description: `Description ${i}`, }, created_at: `2023-09-01T00:00:00-07:00`, }); } const iterations = 5000; const arrayLookupKey = { network: 'network50', metricName: 'metric50', createdAt: '2023-09-01T00:00:00-07:00', }; const arrayStartTime = new Date().getTime(); for (let i = 0; i < iterations; i++) { const arrayResult = rawData.find((entry) => { return ( entry.network === arrayLookupKey.network && entry.metric.name === arrayLookupKey.metricName && entry.created_at === arrayLookupKey.createdAt ); }); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Test using Map for indexing (5000 iterations)
Test using Array.prototype.find (5000 iterations)
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 benchmark and explain what is being tested. **Benchmark Definition:** The benchmark measures the performance of two approaches to look up data in an array: using `Array.prototype.find()` and using a Map for indexing. **Options Compared:** 1. **Array.prototype.find()**: This method searches for an element in the array that satisfies the provided testing function. 2. **Map for indexing**: A Map is used to store key-value pairs, where each key is unique and maps to a specific value. In this case, the key is a JSON string created from the `network`, `metricName`, and `createdAt` properties of an object, and the value is the corresponding `cost_per` property. **Pros and Cons:** * **Array.prototype.find()**: + Pros: - Easy to implement and understand. - Works well for small to medium-sized datasets. + Cons: - Has a linear search time complexity, which can be slow for large datasets. - May have issues with duplicate keys or key collisions. * **Map for indexing**: + Pros: - Fast lookups with an average time complexity of O(1). - Can handle large datasets efficiently. + Cons: - Requires more memory to store the Map. - Creating the JSON key string can be slower than simply accessing the object's properties directly. **Library and Syntax:** The benchmark uses a library called `Map` for indexing, which is a built-in JavaScript data structure. The `JSON.stringify()` function is used to create a unique key from the `network`, `metricName`, and `createdAt` properties of an object. **Special JS Feature/Syntax:** * **Arrow functions**: The benchmark uses arrow functions in some places, such as the `for...of` loop with `transformedDataMap.get()`. Arrow functions are a concise way to define small functions without declaring a separate function scope. * **Template literals**: The benchmark uses template literals, like `network${i}`, to create string values. **Other Alternatives:** If you need to optimize array lookups, other alternatives could be: 1. **Using a binary search tree (BST)**: A BST can provide faster lookup times than an array or Map, especially for large datasets. 2. **Hash tables**: Hash tables are data structures that map keys to values using a hash function, which can provide fast lookups in O(1) time on average. 3. **Sparse arrays**: If the dataset is sparse (i.e., many elements have missing values), you could use a sparse array or a similar data structure to reduce memory usage and improve lookup times. Keep in mind that these alternatives may require more complex implementation and may not be suitable for all use cases. I hope this explanation helps!
Related benchmarks:
set.has vs. array.includes(million)
Array.from() vs new Array() vs [..Array()]
Array.from() vs new Array() - map
Array.from() vs new Array().map()
Array.from() vs new Array() with index
Comments
Confirm delete:
Do you really want to delete benchmark?