Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
10 Objects in a 100 Object Pool; Array find vs Map get
(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 = 100 const objectsToFind = 10 const getRandomNumber = (max) => { return Math.floor(Math.random() * max); } class TestObject { constructor() { this.a = getRandomNumber(randomMax) 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:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:127.0) Gecko/20100101 Firefox/127.0
Browser/OS:
Firefox 127 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.find
1723915.9 Ops/sec
Map.get
124503136.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases. **Benchmark Overview** The benchmark compares the performance of two approaches: `array.find` versus `map.get`. The goal is to find an element in a pool of 100 objects (represented by the `TestObject` class) using either an array or a map. The pool consists of objects with unique identifiers (`a`) and a string property (`b`). **Script Preparation Code** The script preparation code generates: 1. A large number generator (`getRandomNumber`) to create random numbers between 0 and `randomMax`. 2. A `TestObject` class with a constructor that sets the object's properties, including a unique identifier (`a`) and a string property (`b`). 3. An array (`array`) and a map (`map`) initialized with the generated objects. 4. An array of unique identifiers to find (`objectIdsToFind`). **Individual Test Cases** There are two test cases: ### Array.find The `Array.find` test case uses the following code: ```javascript for (let i = 0; i < objectIdsToFind.length; i++) { array.find((val) => val.a === objectIdsToFind[i]) } ``` This code iterates over the `objectIdsToFind` array and for each identifier, it uses the `find` method on the `array` to find an element with a matching `a` property. ### Map.get The `Map.get` test case uses the following code: ```javascript for (let i = 0; i < objectIdsToFind.length; i++) { map.get(objectIdsToFind[i]) } ``` This code iterates over the `objectIdsToFind` array and for each identifier, it uses the `get` method on the `map` to retrieve an object with a matching `a` property. **Pros and Cons** * **Array.find**: Pros: + Efficient for large arrays with unique identifiers. + Can be faster than `Map.get` since it doesn't require extra memory allocation. * Cons: + May perform slower for smaller arrays or when the array is not sorted. + Requires the element to be found in the array, which may lead to unnecessary computations if the element is not present. * **Map.get**: Pros: + Efficient for large datasets with unique identifiers, as it uses a hash table for fast lookups. + Reduces memory allocation and garbage collection compared to `Array.find`. * Cons: + May perform slower for smaller arrays or when the map is not properly indexed. + Requires extra memory allocation for the map. **Library and Special JS Features** The benchmark uses the following JavaScript features: 1. Classes (e.g., `TestObject`) using the `class` keyword. 2. Lambda functions (e.g., `(val) => val.a === objectIdsToFind[i]`) in the `Array.find` test case. These features are part of modern JavaScript and are supported by most browsers. **Alternatives** Other alternatives for implementing this benchmark could be: 1. Using a library like Lodash or Ramda, which provide similar functionality to `Array.find` and `Map.get`. 2. Implementing a custom lookup function using bitwise operations or iteration. 3. Using a different data structure, such as a set or trie, instead of an array or map. However, the benchmark's focus on comparing `array.find` and `map.get`, two built-in methods in JavaScript, makes it a relevant test case for understanding the performance trade-offs between these two approaches.
Related benchmarks:
Map vs Object 5000 rand
1000 Objects in a 10000 Object Pool; Array find vs Map get
1000 Objects in a 10000 Object Pool; Array find vs Map get - using string keys
create map first and find many vs array find many
Comments
Confirm delete:
Do you really want to delete benchmark?