Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.find vs Array.indexOf vs Array.filter vs Map.has
(version: 0)
Compare performance between Array.find vs Map.has
Comparing performance of:
find vs indexOf vs filter vs map
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
let array = []; for (var i=0; i<300; ++i) { array.push('00' + i); } function hasWithFind(needle) { return array.find((entry) => entry == needle) !== undefined; } function hasWithIndexOf(needle) { return array.indexOf(needle) !== -1; } function hasWithFilter(needle) { return array.filter((entry) => entry == needle).length > -1; } let map = {}; array.forEach(item => map[item] = true); function hasWithMap(needle) { return needle in map; }
Tests:
find
for (var i=0; i<100; ++i) { hasWithFind('404'); }
indexOf
for (var i=0; i<100; ++i) { hasWithIndexOf('404'); }
filter
for (var i=0; i<100; ++i) { hasWithFilter('404'); }
map
for (var i=0; i<100; ++i) { hasWithMap('404'); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
find
indexOf
filter
map
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/119.0.0.0 Safari/537.36
Browser/OS:
Chrome 119 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
find
41397.4 Ops/sec
indexOf
49420.9 Ops/sec
filter
30091.1 Ops/sec
map
433176.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and its test cases. **Benchmark Definition** The benchmark compares the performance of four different approaches to check if an element exists in an array: 1. `Array.find()` 2. `Array.indexOf()` 3. `Array.filter()` with a callback function that checks for existence 4. A simple `Map` data structure to store elements and their existence **Options Compared** The benchmark compares the performance of these four approaches in three ways: * **Existence Check**: The time taken to check if an element exists in the array using each approach. * **Frequency**: The number of times each approach is executed (i.e., how many iterations) for a fixed input. **Pros and Cons of Each Approach** 1. **Array.find()**: * Pros: Efficient, as it only scans the array until it finds the element or reaches the end. * Cons: Can throw an error if the element is not found, and may have performance issues with large arrays. 2. **Array.indexOf()**: * Pros: Fast and accurate, but can be slower than `Array.find()` for very large arrays. * Cons: May return -1 instead of a falsey value if the element is not found. 3. **Array.filter() with a callback function**: * Pros: Flexible, as it allows filtering based on multiple conditions. * Cons: Slower than `Array.find()` and `Array.indexOf()`, especially for large arrays. 4. **Map**: * Pros: Fast and efficient, especially for large datasets. * Cons: Requires more memory to store the map data structure. **Library and Purpose** In this benchmark, the `Map` library is used to store elements as keys, which allows for fast existence checks using the `in` operator. The purpose of using a `Map` is to demonstrate its performance benefits in this specific use case. **Special JS Features/Syntax** None of the test cases explicitly uses special JavaScript features or syntax like async/await, generators, or decorators. However, it's worth noting that some of these approaches may be more suitable for certain scenarios where existence checks are used in conjunction with other operations. **Other Alternatives** If you're looking for alternative approaches to check if an element exists in an array, consider: 1. `Array.includes()`: A modern approach that is faster and more readable than `Array.indexOf()` or `Array.find()`. 2. Using a `Set` data structure instead of an array. 3. Using a caching mechanism like Redis or Memcached for large datasets. Keep in mind that the choice of approach depends on the specific use case, performance requirements, and memory constraints.
Related benchmarks:
for vs foreach vs map
for vs foreach vs map 2
Array fill method vs for loop vs map
JS join vs map
Array.map vs preallocation ckck
Comments
Confirm delete:
Do you really want to delete benchmark?