Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test array vs map lookup 3
(version: 0)
asd
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
Script Preparation code:
// Generate an array with 100 objects var 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}`, } }); } // Using a Map for indexing var transformedDataMap = new Map(); for (const entry of rawData) { const key = `${entry.network}_${entry.metric.name}` transformedDataMap.set(key, entry.cost_per); }
Tests:
Test using Map for indexing (5000 iterations)
const iterations = 5000; for (let i = 0; i < iterations; i++) { const mapLookupKey = `network${i}_metric${i}` const mapResult = transformedDataMap.get(mapLookupKey); }
Test using Array.prototype.find (5000 iterations)
const iterations = 5000; const arrayStartTime = new Date().getTime(); for (let i = 0; i < iterations; i++) { const arrayLookupKey = { network: 'network' + i, metricName: 'metric' + i, }; const arrayResult = rawData.find((entry) => { return ( entry.network === arrayLookupKey.network && entry.metric.name === arrayLookupKey.metricName ); }); }
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 provided benchmark and explain what is being tested, compared options, their pros and cons, and other considerations. **Benchmark Definition** The benchmark measures the performance difference between two approaches: 1. **Using a Map for indexing**: The code creates an array with 5001 objects and then iterates over it to create a `Map` object where each key is a combination of the `network` and `metric.name` properties. Then, it uses the `Map` to look up values based on specific keys. 2. **Using Array.prototype.find**: The code creates an array with 5001 objects and then iterates over it to find a specific object that matches certain conditions. **Options Compared** The two options being compared are: * **Map for indexing** + Pros: - Fast lookup times (O(1) on average) - Can handle large datasets + Cons: - Requires more memory to store the `Map` object - Might require additional code to create and manage the keys * **Array.prototype.find**: + Pros: - Lightweight and easy to implement - Supports iteration and filtering out of the box + Cons: - Has a higher overhead due to the need for iterating over the array - May have slower lookup times compared to Map (O(n) on average) **Library Used** In this benchmark, `Map` is used as a library. A `Map` in JavaScript is an object that stores key-value pairs, where each key is unique and maps to a specific value. **Other Considerations** * **JavaScript version**: The benchmark does not specify the exact JavaScript version being used. However, since Chrome 117 is mentioned, it's likely that the benchmark uses a relatively modern version of JavaScript. * **Device and browser**: The benchmark results are reported for a Chrome 117 browser on Linux desktop platforms. This suggests that the benchmark might be optimized for this specific device and browser combination. **Alternatives** Other alternatives to these two options could include: * Using `Object.keys()` and `Array.prototype.forEach` to iterate over an array * Using `JSON.stringify()` and `JSON.parse()` to convert objects to strings and back to objects * Using other data structures, such as a hash table or a trie
Related benchmarks:
Map.get versus Array.find for 100 elements
Map.get versus Array.find for 10000 elements
Map.get versus Array.find for 1000 elements
Search: Array to Map and find vs Array.find 2
map vs reverse for vs for vs push vs unshift
Comments
Confirm delete:
Do you really want to delete benchmark?