Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Usporedba Optimised 100
(version: 0)
Comparing performance of:
Find person in map vs Find person in array
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
window.bigMap = {}; window.bigArray = Array.from({length: 100}).map((_, index) => { const id = "person-id-" + index; const person = { id: id, name: `John Doe ${id}`, age: 18 + Math.floor(Math.random() * 70) }; bigMap[id] = person; return person }) window.generateId = function generateId() { return "person-id-" + Math.floor(Math.random() * 100); } window.getPersonFromMap = function getPersonFromMap() { const id = generateId(); return window.bigMap[id]; } window.getPersonFromArray = function getPersonFromArray() { const id = generateId(); return window.bigArray.find(function findById(p){ return p.id === id; }); } for (let i = 0; i < 10000; i++) { getPersonFromMap() } for (let i = 0; i < 10000; i++) { getPersonFromArray() }
Tests:
Find person in map
getPersonFromMap()
Find person in array
getPersonFromArray()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Find person in map
Find person in array
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/121.0.0.0 Safari/537.36
Browser/OS:
Chrome 121 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Find person in map
3174938.2 Ops/sec
Find person in array
2057381.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what is being tested. **Benchmark Definition** The benchmark definition consists of two parts: 1. **Script Preparation Code**: This code sets up a JavaScript environment with two data structures: an object (`bigMap`) and an array (`bigArray`). The `bigMap` contains 100 entries with randomly generated IDs, while the `bigArray` is populated with 100 objects, each representing a person with a unique ID. Two helper functions are also defined: `generateId()` to generate a random ID and `getPersonFromMap()` to retrieve a person from the map by their ID. 2. **Individual Test Cases**: These test cases measure the performance of two separate functions: * `getPersonFromMap()`: This function retrieves a person from the `bigMap` data structure using the randomly generated ID. * `getPersonFromArray()`: This function retrieves a person from the `bigArray` data structure using the same randomly generated ID. **Options Compared** The benchmark compares two approaches: 1. **Accessing the map**: The first test case measures the performance of accessing a specific key in the `bigMap` object. 2. **Searching the array**: The second test case measures the performance of searching for an element in the `bigArray` using the `find()` method. **Pros and Cons** * **Accessing the map**: + Pros: Direct access to a specific key in the map, potentially faster since it's a native JavaScript object. + Cons: May be slower if the map is large or has many keys, as this approach doesn't benefit from caching or other optimizations. * **Searching the array**: + Pros: Can be faster for larger arrays, as searching can take advantage of caching and other optimizations. + Cons: Slower for small arrays or when accessing a specific element, since it involves iterating over the entire array. **Libraries and Special Features** There are no libraries explicitly mentioned in the benchmark definition. However, the use of `Array.from()` suggests that JavaScript's built-in functions are being leveraged. No special features or syntax are used in this benchmark; it relies solely on standard JavaScript constructs. **Other Alternatives** If you were to rewrite this benchmark with alternative approaches, some options could include: 1. **Using a more efficient data structure**: Consider using a hash table or a database to store the data, which might provide faster lookup times. 2. **Optimizing the search algorithm**: Explore different search algorithms, such as binary searching or interpolation searching, which might be faster for large arrays. 3. **Caching or memoization**: Implement caching or memoization techniques to reduce the number of searches required. Keep in mind that the choice of alternative approaches would depend on the specific requirements and constraints of your project.
Related benchmarks:
Usporedba
Usporedba 2
Usporedba Optimised
Usporedba Optimised 10
Comments
Confirm delete:
Do you really want to delete benchmark?