Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.includes vs Array.find vs Array.indexOf (with string)
(version: 0)
Comparing performance of:
Array.find vs Array.includes vs Array.indexOf
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var values = [] for (var i = 0; i < 1000000; i++) { values.push(`${i}`) }
Tests:
Array.find
var TEST_NUM = '897495' var result = values.find((i) => i === TEST_NUM)
Array.includes
var TEST_NUM = '897495' var result = values.includes(TEST_NUM)
Array.indexOf
var TEST_NUM = '897495' var result = values.indexOf(TEST_NUM) !== -1
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.find
Array.includes
Array.indexOf
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; rv:123.0) Gecko/20100101 Firefox/123.0
Browser/OS:
Firefox 123 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.find
78.4 Ops/sec
Array.includes
182.0 Ops/sec
Array.indexOf
290.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested, along with the pros and cons of each approach. **Benchmark Definition** The benchmark is designed to compare the performance of three different methods for searching an array: 1. `Array.includes()` 2. `Array.find()` 3. `Array.indexOf()` These methods are used to find a specific value within an array. The benchmark creates a large array of 1,000,000 strings and then uses each method to search for a specific string value. **Options Compared** The three options being compared are: 1. **`Array.includes()`**: This method returns `true` if the specified value is found in the array, and `false` otherwise. 2. **`Array.find()`**: This method returns the first element in the array that satisfies the provided testing function. 3. **`Array.indexOf()`**: This method returns the index of the first occurrence of the specified value within the array, or `-1` if not found. **Pros and Cons** * **`Array.includes()`**: + Pros: Simple to implement, fast execution (O(n) average case). + Cons: Can be slower for large arrays, as it scans the entire array to find the match. * **`Array.find()`**: + Pros: Efficient for finding a single value in an array, uses less memory than `includes()`. + Cons: Returns the found value, which might not be useful in all cases. Also, can be slower if the testing function is computationally expensive. * **`Array.indexOf()`**: + Pros: Fast execution (O(n) average case), returns the index of the found value. + Cons: Can throw an error if the array is empty or the specified value is not found. **Library and Special JS Feature** None of these methods use a specific library. However, `Array.find()` uses a functional programming style, which is a modern JavaScript feature that allows for concise and expressive code. **Other Considerations** When choosing between these methods, consider the following: * If you need to find a single value in an array, `Array.indexOf()` might be the best choice. * If you need to find a specific value but don't care about its index or want to return a default value if not found, `Array.includes()` might be sufficient. * If you're working with large arrays and performance is critical, `Array.find()` can be an efficient option. **Alternatives** Other alternatives for searching arrays in JavaScript include: * Using the `indexOf()` method directly on the array (similar to `Array.indexOf()`) * Using a custom loop or recursive function * Using a library like Lodash's `find()` function Keep in mind that these alternatives might not provide the same level of performance as the built-in methods.
Related benchmarks:
Array.includes() vs Array.indexOf()
IndexOf vs Includes array of numbers
Array find with indexOf vs includes
array indexOf vs includes vs some v3
find vs includes vs indexof
Comments
Confirm delete:
Do you really want to delete benchmark?