Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.includes vs Array.find vs Array.indexOf
(version: 0)
Comparing performance of:
Array.find vs Array.includes vs Array.indexOf
Created:
5 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(v => v === 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:
16 days ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/148.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 148 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.find
24240254.0 Ops/sec
Array.includes
39641360.0 Ops/sec
Array.indexOf
39479924.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark. **Benchmark Definition** The benchmark measures the performance of three different methods for finding an element in an array: `Array.includes`, `Array.indexOf`, and `Array.find`. These methods are all used to search for a specific value within the large array created by the script preparation code. **Script Preparation Code** The script generates a large array of 1 million elements, each with a unique index. This is done using a simple JavaScript loop: ```javascript for (var i = 0; i < 1000000; i++) { values.push[i] = i; } ``` This creates an array where each element has the same value as its index. **Html Preparation Code** There is no HTML preparation code provided, so we can assume that this benchmark only tests JavaScript performance and does not involve any web page rendering or other external factors. **Individual Test Cases** The three test cases are: 1. `Array.find`: This method uses a callback function to find the first element in the array that satisfies a condition. ```javascript var TEST_NUM = 897495; var result = values.find(v => v === TEST_NUM); ``` 2. `Array.includes`: This method checks if an element with the specified value exists in the array. ```javascript var TEST_NUM = 897495; var result = values.includes(TEST_NUM); ``` 3. `Array.indexOf`: This method returns the index of the first occurrence of the specified value, or -1 if it is not found. ```javascript var TEST_NUM = 897495; var result = values.indexOf(TEST_NUM) !== -1; ``` **Pros and Cons** Here's a brief summary of each method: * `Array.includes`: + Pros: Simple and efficient, with an average time complexity of O(n). + Cons: May not be suitable for large arrays or performance-critical applications. * `Array.indexOf`: + Pros: Can be used to find the index of any element, not just a specific value. + Cons: Returns -1 if the element is not found, which may require additional checks in some cases. Average time complexity is O(n). * `Array.find`: + Pros: Finds the first matching element and returns it, making it useful for searching arrays. + Cons: May be slower than `includes` or `indexOf` due to its extra overhead. Average time complexity is O(n). **Library and Syntax** There are no libraries used in this benchmark. **Special JS Features** None mentioned. **Other Alternatives** Some alternative methods for finding an element in an array include: * Using a `for` loop with indexing: `var result = 0; while (result < values.length && values[result] !== TEST_NUM) { result++; }` * Using the `includes()` method with a callback function: `values.includes(v => v === TEST_NUM)` * Using the `some()` and `findIndex()` methods: `values.some((v, i) => v === TEST_NUM);` or `values.findIndex((v, i) => v === TEST_NUM)` However, these alternatives are not part of the standard JavaScript array API and may have different performance characteristics.
Related benchmarks:
Array construct vs array push
Clear array via array = [] vs array.length = 0
Array.from() vs new Array() vs push
Array.from() vs new Array() vs push pushup
Array.push(x) vs array[n]=x
Comments
Confirm delete:
Do you really want to delete benchmark?