Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
indexOf vs includes vs findIndex
(version: 0)
Comparing performance of:
indexOf vs includes vs findIndex
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var len = 10000 var arr = [] var randInd = Math.random() * 10000 // Setup out test objects w/ random values for (let i = 0; i < len; i++) { arr.push(Math.random()) }
Tests:
indexOf
arr.indexOf(randInd)
includes
arr.includes(randInd)
findIndex
arr.findIndex((e) => e == randInd)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
indexOf
includes
findIndex
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. **What is being tested?** The benchmark measures the performance of three different methods for searching an element in an array: 1. `arr.indexOf(randInd)` 2. `arr.includes(randInd)` 3. `arr.findIndex((e) => e == randInd)` These methods are used to find the index of a specific value (`randInd`) within the randomly generated array (`arr`). **Options being compared** The three options being compared are: 1. **`indexOf`**: Returns the index of the first occurrence of the specified value (`randInd`). If no match is found, it returns `-1`. 2. **`includes`**: Returns a boolean indicating whether the array includes the specified value (`randInd`). It's essentially doing a fast check to see if `randInd` exists in the array. 3. **`findIndex`**: Similar to `indexOf`, but it returns the index of the first occurrence of the specified value (`randInd`) or `-1` if no match is found. **Pros and Cons** Here are some pros and cons for each approach: * **`indexOf`**: + Pros: Fast, simple, and widely supported. + Cons: Returns the index of the first occurrence, which might not be what you want. Also, it's not as efficient as `includes` for large arrays. * **`includes`**: + Pros: Fast, modern, and widely supported (since ES6). It also returns a boolean value, making it more convenient in some cases. + Cons: Not as efficient as `indexOf` for small arrays or when performance is critical. Also, it's not suitable for finding the index of the first occurrence. * **`findIndex`**: + Pros: Returns the index of the first occurrence, making it useful when you need to know the position of the element in the array. + Cons: Less efficient than `indexOf` and `includes`. Also, it's not as widely supported. **Library and purpose** None of these methods use a library. They are all built-in JavaScript functions or methods that can be used directly in your code. **Special JS feature or syntax** There is no specific special JavaScript feature or syntax being tested here. The benchmark focuses solely on the performance of different search methods for arrays. **Other alternatives** If you need to search an array and want alternative solutions, consider: * **`Array.prototype.some()`**: Similar to `includes`, but returns a boolean value indicating whether at least one element matches the condition. * **`Array.prototype.find()`**: Returns the first element that satisfies the provided testing function. It's similar to `findIndex`, but it can also return `undefined` if no match is found. Keep in mind that these alternatives might not be as efficient as `indexOf` and `includes` for certain use cases, but they offer different trade-offs depending on your specific requirements.
Related benchmarks:
findIndex vs indexOf on array of objs
indexOf vs findIndex with a simple case
findIndex vs indexOf for simple array 2
findIndex vs indexOf vs includes - JavaScript performance
Comments
Confirm delete:
Do you really want to delete benchmark?