Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array find vs Map get I
(version: 1)
Comparing performance of:
array find vs map get
Created:
10 months ago
by:
Guest
Jump to the latest result
Script Preparation code:
const arr = Array.from({ length: 1000 }, () => Math.floor(Math.random() * 100) + 1 );
Tests:
array find
for (var i = 0; i < 1000; i++) { arr.find(v => v === i) }
map get
const m = new Map() arr.forEach(v => { m.set(v, v) }) for (var i = 0; i < 1000; i++) { m.get(i) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array find
map get
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36
Browser/OS:
Chrome 140 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array find
2377.5 Ops/sec
map get
118966.4 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 10 months ago):
The benchmark titled "Array find vs Map get I" compares the performance of two different methods of retrieving elements from a collection in JavaScript: using the `Array.prototype.find` method and using a `Map` object with its `get` method. ### Options Compared 1. **Array `find` Method** - **Test Code**: ```javascript for (var i = 0; i < 1000; i++) { arr.find(v => v === i); } ``` - **Description**: This method iterates over the `arr` array and checks if any element matches the condition defined by the predicate function (in this case, whether the element is equal to `i`). If a match is found, it returns that element; otherwise, it continues checking until the end of the array. 2. **Map `get` Method** - **Test Code**: ```javascript const m = new Map(); arr.forEach(v => { m.set(v, v); }); for (var i = 0; i < 1000; i++) { m.get(i); } ``` - **Description**: This method creates a `Map` object, where each element from the `arr` is added with itself as both key and value. The benchmark then iterates from 0 to 999 and retrieves the values using `m.get(i)`. The `get` method checks for the presence of the key `i` in the `Map`, which is generally faster than searching through an array. ### Pros and Cons #### Array `find` - **Pros**: - Simplicity: The syntax is straightforward and easy to read, making it clear what the code is doing. - No additional data structure is needed; it works directly on existing arrays. - **Cons**: - Performance: Searching through an array can be slow because it requires a linear scan of the array (O(n) time complexity) in the worst case. - For large datasets, this method can become a bottleneck. #### Map `get` - **Pros**: - Performance: Maps provide average time complexity of O(1) for retrievals, which makes them significantly faster than searching through arrays, especially when the data size increases. - Better suited for frequent lookups, as it uses hashing behind the scenes. - **Cons**: - Overhead: There is additional memory usage because it involves creating a new `Map` object and storing all elements in it. - Slightly more complex code, as it requires setting up the `Map`. ### Additional Considerations - **Use Case**: Choose `find` for smaller datasets or when readability is a priority. Opt for `Map` when performance is critical, especially in applications where frequent and rapid lookups are required. - **Alternatives**: Other alternatives might include using a plain object for key-value pairs instead of `Map`, but `Map` offers better semantics and a richer feature set (like maintaining insertion order). ### Benchmark Results The benchmark results showed: - The `Map get` approach was significantly faster, achieving approximately 51,425 executions per second. - The `Array find` method was much slower, with only about 433 executions per second. This stark difference highlights the efficiency of using a `Map` for lookups over conventional array traversal when the order of operations and performance is critical, particularly in large datasets.
Related benchmarks:
Array.find vs Object
[test] for vs find
loop vs map 1994
map and find may vs array find may
array to map and find small vs array find small
array to map and find small vs array find small again
Map get VS Map has get 4
indexOf vs map 002-5f
Get index with indexOf or with map
Comments
Confirm delete:
Do you really want to delete benchmark?