Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
IndexOf vs Includes in function
(version: 1)
Banana
Comparing performance of:
IndexOf vs Includes
Created:
9 months ago
by:
Guest
Jump to the latest result
Script Preparation code:
var needle = () => {} var arr = [() => {}, () => {}, () => {}, () => {}, () => {}, () => {}, () => {}, needle, () => {}, () => {}, () => {}, () => {}, () => {}]
Tests:
IndexOf
arr.indexOf(needle)
Includes
arr.includes(needle)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
IndexOf
Includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:147.0) Gecko/20100101 Firefox/147.0
Browser/OS:
Firefox 147 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
IndexOf
29325126.0 Ops/sec
Includes
31020062.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 9 months ago):
The benchmark provided compares the performance of two JavaScript methods for searching an element in an array: `indexOf` and `includes`. ### Options Compared 1. **`arr.indexOf(needle)`** - **Description**: The `indexOf` method searches the array for a specified value and returns its first index if found. If the value is not found, it returns -1. - **Test Name**: "IndexOf" 2. **`arr.includes(needle)`** - **Description**: The `includes` method determines whether an array includes a certain value among its entries, returning true or false as appropriate. - **Test Name**: "Includes" ### Pros and Cons of Each Approach #### `indexOf` - **Pros**: - It is widely supported in all JavaScript environments, including very old versions. - Can be used for both value checking and index retrieval, making it versatile. - **Cons**: - Returns -1 if the item is not found, which can sometimes lead to confusion since -1 might be a valid index in some scenarios. - A broader set of checks is required when determining if the element is present, as you need to handle the possibility of -1. #### `includes` - **Pros**: - More intuitive to use when you only need to check if an array contains a specific value. - Returns a boolean (true or false), making it easier to use in conditional expressions, thus improving code readability. - **Cons**: - May have slightly less support in older JavaScript environments (though this is becoming less of an issue as older browsers fall out of use). - Assumes the presence of `Object.is` semantics for strict comparison, which could affect the results when dealing with unique reference types or NaN. ### Considerations When deciding to use one method over the other, consider the context in which you are working. If you require index information, `indexOf` may be the right choice. However, if you're simply concerned with the presence of an element, `includes` provides cleaner and more readable code. ### Benchmark Results The benchmark results show that `indexOf` performs slightly better than `includes` in terms of executions per second: - **IndexOf**: 97,362,200 executions per second - **Includes**: 92,688,224 executions per second While `indexOf` shows better performance in this benchmark, it’s essential to note that performance can vary based on various factors, including the specific JavaScript engine implementation and array size. In many practical applications, performance differences might be negligible for smaller datasets or less frequent operations. ### Alternative Approaches 1. **Using a Loop**: For very specific use cases, manually iterating through an array using a loop can be used for searching elements. This method can provide more control over how comparisons are made but at a cost of increased complexity and usually slower performance. 2. **Using a Set**: If duplicate values are not needed and you want faster lookups, consider using a `Set` for membership checks. Sets offer average-time complexity of O(1) for lookups, which can significantly improve performance in cases where you need to check membership frequently. In conclusion, both `indexOf` and `includes` have their particular use cases in JavaScript development, and the choice between them should be guided by the specific requirements of the application and the importance of code readability versus raw performance.
Related benchmarks:
Test includes versus indexof
Array.includes() vs. manual compare
IndexOf > vs Includes
test 1..
Js Search - IndexOf vs Includes
has vs includes
IndexOf vs Includes in object
String.indexOf vs Array.indexOf
IndexOf vs Includes in array by miette
Comments
Confirm delete:
Do you really want to delete benchmark?