Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS find vs indexOf vs includes
(version: 0)
JS find vs indexOf
Comparing performance of:
Array.find vs Array.indexOf vs includes
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = []; var i = 0; while (i <= 1E5) arr[i] = i++;
Tests:
Array.find
const item = arr.find(item => item == 1E5);
Array.indexOf
const index = arr.indexOf(1E5);
includes
const index = arr.includes(1E5);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Array.find
Array.indexOf
includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.find
1279.6 Ops/sec
Array.indexOf
71490.3 Ops/sec
includes
71312.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark. **Benchmark Purpose:** The purpose of this benchmark is to compare the performance of three JavaScript methods for finding an element in an array: `find()`, `indexOf()`, and `includes()`. **Methods Compared:** 1. **Array.find()**: This method returns the value of the first element in the array that satisfies the provided condition. 2. **Array.indexOf()**: This method returns the index of the first occurrence of a specified value in the array. 3. **String.includes()**: This method tests whether an array includes a specified value. **Pros and Cons:** * **Array.find():** + Pros: - Can be used to find any type of data, not just numbers. - Returns the actual found element, which can be useful in some cases. + Cons: - May be slower than `indexOf()` for finding a single value because it needs to traverse the entire array to find the first match. * **Array.indexOf():** + Pros: - Fast and efficient for finding a single value in an array. + Cons: - Returns -1 if the element is not found, which can be considered as "failure" in some cases. * **String.includes():** + Pros: - Similar to `indexOf()`, but for strings. + Cons: - May be slower than `indexOf()` because it needs to check all characters in the array. **Library/Functionality Used:** * None. The benchmark only tests native JavaScript methods. **Special JS Features/Syntax:** The benchmark uses modern JavaScript syntax, such as arrow functions (`item => item == 1E5`) and template literals (`const index = arr.indexOf(1E5);`). **Other Alternatives:** * **Array.prototype.some()**: This method returns a boolean indicating whether at least one element in the array satisfies the provided condition. * **Array.prototype.every()**: This method returns a boolean indicating whether all elements in the array satisfy the provided condition. These methods are not included in the benchmark, but they could be used as alternatives to `find()`, `indexOf()`, and `includes()` depending on the specific use case. In general, the choice of method depends on the specific requirements of the application. For example: * Use `find()` when you need to find a single element with a certain property. * Use `indexOf()` or `includes()` when you need to quickly search for a value in an array and don't care about finding all matches. * Use `some()` or `every()` when you need to check if at least one or all elements in the array satisfy a condition, respectively.
Related benchmarks:
JS find vs indexOf
findIndex vs indexOf for simple array 2
JS Array IndexOf vs includes vs findIndex vs find
Array find with indexOf vs includes
JS Array IndexOf vs includes vs findIndex vs find 2
Comments
Confirm delete:
Do you really want to delete benchmark?