Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.find vs. Map.get 20241610
(version: 0)
Test the performance of Array.find vs. Map.get, with various array sizes, and with/without conversion from array to map
Comparing performance of:
Array.find, 10 000 elements vs Map.get, 10 000 elements
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
function getRandomElement(id) { return { id, a: Math.random(), b: Math.random(), c: Math.random(), } } function getArray(length) { const result = []; for (let i = 0; i < length; i++) { result.push(getRandomElement(i)) } return result; } function arrayToMap(array) { return new Map(array.map(el => [el.id, el])); } function getRandomInt(max) { return Math.floor(Math.random() * max); } array_small = getArray(10000); array_large = getArray(1000000); map_small = arrayToMap(array_small); map_large = arrayToMap(array_large);
Tests:
Array.find, 10 000 elements
const target = getRandomInt(9999); for (let i = 0; i < 1000; i++) { array_small.find(el => el.id === target); }
Map.get, 10 000 elements
map_small = arrayToMap(array_small); for (let i = 0; i < 1000; i++) { const target = getRandomInt(9999); map_small.get(target); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.find, 10 000 elements
Map.get, 10 000 elements
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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.find, 10 000 elements
98.2 Ops/sec
Map.get, 10 000 elements
498.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided JSON represents a benchmark definition for comparing the performance of `Array.find` versus `Map.get` with various array sizes, as well as with/without conversion from array to map. **Tested Options** The benchmark compares two options: 1. **Array.find**: A method that searches for an element in an array based on a predicate function. 2. **Map.get**: A method that retrieves the value associated with a given key from a Map object. **Pros and Cons of Each Approach** * **Array.find**: * Pros: Simple, efficient, and widely supported by most JavaScript engines. * Cons: Can be slower for very large arrays due to its iterative approach. It's also less memory-efficient than `Map.get` because it creates a new array of indices that match the desired element. * **Map.get**: * Pros: More efficient and memory-friendly, especially for large datasets. It uses a hash table data structure under the hood, which provides faster lookup times. * Cons: Less widely supported by older JavaScript engines, requires an additional Map object creation. **Library Used** In this benchmark, the `arrayToMap` function is used to create a Map object from an array. This library facilitates converting arrays to Maps for easier comparison of their performance with `Array.find`. ```javascript function arrayToMap(array) { return new Map(array.map(el => [el.id, el])); } ``` **Other Considerations** * The benchmark uses random element generation (`getRandomElement`) and array size variation (`getArray`) to ensure that the results are representative of real-world scenarios. * The `getRandomInt` function is used to generate random integers for testing purposes. **Alternatives** For a more comprehensive comparison, consider adding additional benchmarks for other operations, such as: * **Array.forEach**: Iterating over an array using `forEach`. * **Set.has**: Checking if an element exists in a Set object. * **DOM manipulation**: Performing DOM-related tasks, like traversing or searching elements. By expanding the benchmark to cover more scenarios, you can gain a better understanding of how different JavaScript engines and libraries perform under various workloads.
Related benchmarks:
Array.find vs. Map.getss
Array.find vs. Map.get 2
Array.find vs. Map.get 300
Array.find vs. Map.get 20241610 2
Comments
Confirm delete:
Do you really want to delete benchmark?