Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
1000 Objects in a 10000 Object Pool; Array find vs Map get - using string keys
(version: 0)
Comparing array find vs map get
Comparing performance of:
Array.find vs Map.get
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
const randomMax = 100000000 const objectPoolSize = 10000 const objectsToFind = 1000 const getRandomNumber = (max) => { return Math.floor(Math.random() * max); } function getRandomString() { return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5) + Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5) + Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5) + Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5) } class TestObject { constructor() { this.a = getRandomString() this.b = 'Test String' } } var map = new Map() var array = [] for (let i = 0; i < objectPoolSize; i++) { const testObject = new TestObject() map.set(testObject.a, testObject) array.push(testObject) } var objectIdsToFind = []; for (let i = 0; i < objectsToFind; i++) { objectIdsToFind.push(array[getRandomNumber(array.length)].a) }
Tests:
Array.find
for (let i = 0; i < objectIdsToFind.length; i++) { array.find((val) => val.a === objectIdsToFind[i]) }
Map.get
for (let i = 0; i < objectIdsToFind.length; i++) { map.get(objectIdsToFind[i]) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.find
Map.get
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 what's being tested in this benchmark. **Benchmark Overview** The benchmark compares the performance of two approaches: `array.find()` and `map.get()`. Both methods are used to find objects in an array or map by matching their IDs, which are strings generated randomly. The goal is to determine which method is faster for large datasets. **Array.find()** `array.find()` iterates through the array until it finds a match with the specified ID. If no match is found, it returns `undefined`. Pros: * Simple and straightforward implementation * Works well for small to medium-sized arrays Cons: * Can be slow for very large arrays due to its iterative nature * Requires checking each element individually, which can lead to performance issues if the array is too large **Map.get()** `map.get()` directly accesses the value associated with a given key (in this case, an ID) in the map. If no match is found, it returns `undefined`. Pros: * Can be faster than `array.find()` for large arrays because it uses a hash table to quickly look up values by key * Reduces overhead compared to iterating through an array Cons: * Requires creating a map first, which can add overhead for small datasets * May not perform well if the map is not properly indexed or if the keys are not evenly distributed **Other Considerations** In this benchmark, we also see that each object has multiple random strings as properties (a, b, c, d). While these additional properties don't directly affect the performance of `array.find()` and `map.get()`, they do contribute to the overall size of the dataset. **Library Usage** The library used here is JavaScript's built-in `Map` and `Array` data structures. **Special JS Feature/Syntax** There are no special features or syntax used in this benchmark. The code uses standard JavaScript concepts, such as loops, functions, and object creation. Now, let's talk about alternatives: 1. **Using a different data structure**: Instead of using an array and map, you could use a different data structure like a `Set` or a `TreeMap`. These data structures might offer better performance for certain types of queries. 2. **Caching**: If the dataset is static and won't change frequently, you could consider caching the results of `map.get()` to avoid repeated lookups. 3. **Using an optimization technique**: Depending on the specific requirements of your use case, you might be able to optimize the performance of either `array.find()` or `map.get()` by using techniques like memoization, lazy evaluation, or parallel processing. Keep in mind that these alternatives might not necessarily improve performance for this specific benchmark, but they could be worth exploring if you need to optimize similar operations in your own code.
Related benchmarks:
1000 Objects in a 10000 Object Pool; Array find vs Map get
10 Objects in a 100 Object Pool; Array find vs Map get
10000 Object Pool; Array vs Map Memory V3
Object.create(null) vs {} vs Map() key access (heavy)
Comments
Confirm delete:
Do you really want to delete benchmark?