Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test array vs map lookup 2
(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}`, }, created_at: `2023-09-01T00:00:00-07:00`, }); } // Using a Map for indexing var 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); }
Tests:
Test using Map for indexing (5000 iterations)
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)
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 dive into the Benchmark Definition JSON and analyze what's being tested. **Benchmark Description** The benchmark is designed to compare two approaches for lookup operations on an array of objects: 1. Using a `Map` data structure for indexing 2. Using the `Array.prototype.find()` method for searching in the array **Test Case 1: Map Lookup** The first test case uses a `Map` to store indices corresponding to specific network, metric name, and creation date combinations. The `transformedDataMap.get(mapLookupKey)` expression retrieves the cost per value associated with each key. Pros of using a `Map`: * Fast lookup time (O(1) on average) * Efficient use of memory, as only relevant data is stored Cons: * May have higher overhead due to object creation and manipulation * Requires manual key generation and management **Test Case 2: Array.prototype.find()** The second test case uses the `Array.prototype.find()` method to search for an object in the array based on specific criteria. The expression `rawData.find((entry) => { ... })` returns the first matching entry. Pros of using `Array.prototype.find()`: * Familiar and widely supported API * Easy to read and maintain Cons: * Generally slower than `Map` lookup (O(n) in worst-case scenarios) * May have higher overhead due to array scanning **Library Usage** The benchmark uses the following libraries: * None explicitly mentioned, but it's likely that the standard JavaScript libraries are being used. **Special JS Features/Syntax** None are mentioned specifically. However, the use of template literals (`\r\n network${i}\r\n`, `\r\n metricName: 'metric${i}'\r\n`) and the `JSON.stringify()` method might be considered advanced or niche features. **Alternatives** Other approaches for lookup operations in arrays include: * Using an object with multiple properties to store indices (e.g., a nested object) * Implementing a custom search algorithm using bitwise operations * Utilizing libraries like Lodash or Ramda, which provide optimized searching and filtering functions Keep in mind that the choice of approach depends on the specific requirements and constraints of your use case.
Related benchmarks:
Object spread vs New map entries
test array vs map lookup
test array vs map lookup 3
map vs reverse for vs for vs push vs unshift
Comments
Confirm delete:
Do you really want to delete benchmark?