Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.find vs. Map.get 20241610 2
(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(100); array_large = getArray(1000000); map_small = arrayToMap(array_small); map_large = arrayToMap(array_large);
Tests:
Array.find, 10 000 elements
const target = getRandomInt(99); for (let i = 0; i < 100; i++) { array_small.find(el => el.id === target); }
Map.get, 10 000 elements
map_small = arrayToMap(array_small); for (let i = 0; i < 100; i++) { const target = getRandomInt(99); 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:
5 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:140.0) Gecko/20100101 Firefox/140.0
Browser/OS:
Firefox 140 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.find, 10 000 elements
57209.0 Ops/sec
Map.get, 10 000 elements
151109.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what is being tested in the provided JSON. The benchmark compares the performance of two approaches: 1. **Array.find**: This method searches for an element in an array that satisfies a condition (in this case, `el.id === target`). The search starts from the beginning of the array and moves forward until it finds a match or reaches the end. 2. **Map.get**: This method retrieves the value associated with a given key from a Map object. **Options compared:** * Array size: Two sizes are used: 100 elements (`array_small`) and 1,000,000 elements (`array_large`). * Conversion from array to map: Some tests use conversion from array to map (`map_small` and `map_large`), while others do not. **Pros and cons of each approach:** * **Array.find**: + Pros: - Simple implementation. - Can be useful when the data is small or sparse, as it avoids unnecessary memory allocation. + Cons: - Slower for large datasets due to the need to scan the entire array. - May cause issues if the condition is not properly optimized (e.g., using `Array.prototype.indexOf()` instead of `Array.prototype.find()`). * **Map.get**: + Pros: - Faster for large datasets, as it uses a hash table data structure that allows for efficient lookups. - More memory-efficient than arrays, especially for dense data. + Cons: - Requires more memory to store the Map object and its entries. - May have additional overhead due to the creation of a new object. **Library/Tool usage:** The benchmark uses the following libraries: * None (native JavaScript methods). **Special JS feature/syntax:** None mentioned in this explanation, but some of these features might be used in other tests or benchmarks. For example, `getRandomElement` and `arrayToMap` are custom functions that create random data for testing purposes. **Other alternatives:** For large-scale performance comparisons, you might also consider: * Using a benchmarking framework like Benchmark.js or micro-benchmark. * Utilizing WebAssembly (WASM) benchmarks to compare the performance of different JavaScript engines. * Leveraging multi-threading or parallel processing techniques to test concurrent execution of Array.find and Map.get. Keep in mind that these alternatives may have additional complexity, setup requirements, and trade-offs compared to the simple JSON-based benchmark provided.
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
Comments
Confirm delete:
Do you really want to delete benchmark?