Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Usporedba Optimised
(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: 1000}).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() * 1000); } 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
2939577.8 Ops/sec
Find person in array
382074.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its components. **Benchmark Definition** The benchmark is designed to measure the performance of two different approaches: finding an element in a Map data structure (`getPersonFromMap()`) versus finding an element in an array (`getPersonFromArray()`). **Script Preparation Code** This code sets up a JavaScript environment with: 1. A `bigMap` object, which is an empty Map that will be populated with 10,000 objects. 2. An `Array.from()` function used to generate a large array of 10,000 objects, each representing a person with a unique ID and name. 3. Two functions: `generateId()`, which generates a random ID, and `getPersonFromMap()`, which retrieves an object from the Map using its generated ID. 4. Another function, `getPersonFromArray()`, which uses the `Array.prototype.find()` method to find an object in the array that matches the generated ID. **Options Compared** The two options being compared are: 1. **Finding an element in a Map (`getPersonFromMap()`)**: This approach uses a hash table (Map) data structure, where each key-value pair is stored in memory. 2. **Finding an element in an array (`getPersonFromArray()`)**: This approach uses a dynamic array data structure, which grows or shrinks as elements are added or removed. **Pros and Cons** * **Finding an element in a Map:** + Pros: - Fast lookups (average time complexity of O(1) due to hash table indexing). - Memory-efficient, as the number of keys is typically much smaller than the value set. + Cons: - Requires a good hash function to minimize collisions. - May not be suitable for large datasets or sparse data, where some keys may never be used. * **Finding an element in an array:** + Pros: - Suitable for large datasets and sparse data, as the number of elements is often much larger than the desired subset. - Can be more intuitive to implement, especially when working with iterative algorithms. + Cons: - Time complexity can vary depending on the implementation (e.g., linear search in O(n) time). - May require additional memory for indices or pointers. **Library and Purpose** The `Array.from()` function is a built-in JavaScript method used to create a new array from an iterable source. In this benchmark, it's used to generate a large array of objects. No external libraries are required in this benchmark. **Special JS Feature/ Syntax** None mentioned, but it's worth noting that the use of `Array.prototype.find()` is a relatively modern JavaScript feature introduced in ECMAScript 2015 (ES6). This method provides a concise way to find an element in an array, making the code more expressive and easier to read. **Other Alternatives** For finding elements in a Map or array, other approaches might include: 1. Using `Object.keys()` or `Array.prototype.indexOf()` for arrays. 2. Implementing a custom hash function or collision resolution strategy for Maps. 3. Utilizing data structures like BSTs (Binary Search Trees) or tries for more efficient lookup. However, these alternatives are typically less common or less suitable than the Map and array approaches used in this benchmark. Overall, this benchmark provides a useful comparison of two fundamental JavaScript concepts: working with data structures like Maps and arrays to find specific elements efficiently.
Related benchmarks:
Usporedba
Usporedba 2
Usporedba Optimised 100
Usporedba Optimised 10
Comments
Confirm delete:
Do you really want to delete benchmark?