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
(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); } 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 (X11; Linux x86_64; rv:128.0) Gecko/20100101 Firefox/128.0
Browser/OS:
Firefox 128 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.find
10.9 Ops/sec
Map.get
788690.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain what's being tested in the provided benchmark. **Benchmark Description** The benchmark is designed to compare the performance of two JavaScript methods: `array.find()` and `map.get()`. The goal is to find an object with a specific property value (`a`) within large arrays and maps. **Test Case 1: Array.find()** * The test case creates a pool of 10,000 objects (each with a unique `a` property) in an array. * It then generates 1,000 random object IDs to search for in the array. * The test runs the `array.find()` method, which iterates through the array and returns the first object whose `a` property matches one of the generated object IDs. **Test Case 2: Map.get()** * Similar to Test Case 1, but instead of an array, a map is used to store the objects. * The same random object IDs are used to search for corresponding values in the map. **Options Compared** The benchmark compares the performance of two approaches: 1. **Array.find():** * Pros: + Simple and efficient for small to medium-sized datasets. * Cons: + May not perform well for very large datasets, as it has a time complexity of O(n). 2. **Map.get():** * Pros: + More efficient than `array.find()` for large datasets, with an average time complexity of O(1) due to the use of hash tables. * Cons: + Requires more memory to store the map and its key-value pairs. **Library Usage** In this benchmark, no external libraries are used beyond the built-in JavaScript objects (array and map). **Special JS Features/Syntax** None are explicitly mentioned in the provided code. However, it's worth noting that the use of `const` and `let` declarations, as well as arrow functions (`=>`), is a relatively modern feature introduced in ECMAScript 2015. **Other Alternatives** While not directly compared in this benchmark, other methods for searching large arrays and maps might be considered: * Using `array.indexOf()` instead of `array.find()` * Implementing custom search algorithms (e.g., binary search) * Utilizing third-party libraries or data structures optimized for fast lookup and insertion Keep in mind that the choice of algorithm and implementation details can significantly impact performance, so it's essential to evaluate and optimize your specific use case.
Related benchmarks:
Map vs Object 5000 rand
10 Objects in a 100 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?