Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
find faster - array.find vs map.get
(version: 1)
Comparing performance of:
find vs map
Created:
8 months ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = [] let i = 0 let len = 100 for(;i<len;i++) { arr.push({ key:i, value: i}) }
Tests:
find
arr.find(({key}) => key === 90) arr.find(({key}) => key === 91) arr.find(({key}) => key === 92) arr.find(({key}) => key === 93) arr.find(({key}) => key === 94) arr.find(({key}) => key === 95)
map
const map = arr.reduce((acc, item) => { acc.set(item["key"], item["value"]); return acc; }, new Map()) map.get(90) map.get(91) map.get(92) map.get(93) map.get(94) map.get(95)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
find
map
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36 Edg/141.0.0.0
Browser/OS:
Chrome 141 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
find
3453637.2 Ops/sec
map
762090.2 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 8 months ago):
The benchmark provided tests the performance of two different methods for searching through an array of objects in JavaScript: the `Array.prototype.find` method and the `Map` object's `get` method. ### Benchmarked Options: 1. **Array.find**: - **Description**: This method is used to find a single element in an array that satisfies a specified condition, expressed as a function. In this case, the condition checks if the `key` of each object in the array matches a specific number (ranging from 90 to 95). - **Pros**: - Simple and direct to use for small arrays or scenarios where only a few lookups are needed. - No need to pre-process the data; you can use it directly on any array of objects. - **Cons**: - Since `find` scans the array sequentially, its time complexity is O(n) for each lookup where n is the length of the array. This results in worse performance as array size grows. 2. **Map.get**: - **Description**: This method retrieves a value associated with a specified key in a `Map` object. In this benchmark, a `Map` is created from the original array using `Array.prototype.reduce`, where each object in the array is used to populate the map based on its `key` and `value` properties. After it's created, `get` is called to retrieve values for keys 90 through 95. - **Pros**: - Provides O(1) average time complexity for lookups thanks to the internal structure of `Map`, which allows for fast retrieval of values based on keys. - More efficient than `find` for numerous lookups in larger datasets, as it dramatically reduces the time taken for each search after an initial setup. - **Cons**: - Requires additional memory due to the creation of the `Map`. - There is an upfront cost of constructing the map, which takes O(n) time during the `reduce` operation. ### Considerations: - **Performance**: In scenarios where a large number of lookups are performed, using a `Map` is significantly more performant than repeatedly using `Array.find`. - **Array Size**: If the array is small or if lookups are infrequent, then using `find` might still be a simpler option. - **Memory Usage**: The memory overhead of storing a `Map` may be unwanted in memory-constrained environments or for small datasets. ### Alternatives: - **Linear Search**: Any naive approach where you iterate through the array without the `find` method will have similar performance to `Array.find`, but with more boilerplate. - **Object Lookup**: Instead of using a `Map`, an object can be used for similar key-value lookups, providing fast retrieval but with greater risk of key collisions and less formal structure than a `Map`. - **Binary Search**: If the array can be sorted and the dataset is static (not changing often), implementing a binary search on a sorted array could be an alternative, although this requires that the data is initially sorted and does not benefit from existing object structures. This benchmark ultimately illustrates the importance of selecting the right data structure and method for the task at hand, focusing on time complexity versus memory requirements to achieve optimal performance in JavaScript applications.
Related benchmarks:
[test] for vs find
find faster - for vs find
find faster - for vs find vs map vs foreach
for vs find vs map vs foreach just longer
is find faster than forEach? fork
some vs find loop
find faster - for vs find vs map vs foreach v2
js array comparison - for vs find vs map vs foreach
Array find vs Map get I
Comments
Confirm delete:
Do you really want to delete benchmark?