Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array includes vs Array.indexOf
(version: 0)
Comparing performance of:
Array.includes vs Array.indexOf
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
Array.includes
const markets = ["DE","FR", "ES", "UK", "IT", "MX", "CA", "US"]; markets.includes('UK');
Array.indexOf
const markets = ["DE","FR", "ES", "UK", "IT", "MX", "CA", "US"]; markets.indexOf('UK') > 0;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.includes
Array.indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.includes
88703968.0 Ops/sec
Array.indexOf
86854328.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmarking test cases. **Benchmark Definition** The `Name` and `Description` fields are empty, indicating that no specific purpose or context is given for this benchmark. The `Script Preparation Code` and `Html Preparation Code` fields are also blank, suggesting that no additional setup or configuration is required before running the tests. **Individual Test Cases** There are two test cases: 1. **Array.includes** This test case involves creating an array of market codes (`markets`) and then checking if a specific code (`'UK'`) is included in the array using the `includes()` method. 2. **Array.indexOf** This test case also creates an array of market codes (`markets`) but checks for the presence of a specific code (`'UK'`) by calling the `indexOf()` method and verifying that its return value is greater than 0. **Options Compared** The two options being compared are: 1. Using the `includes()` method to check if an element is present in an array. 2. Using the `indexOf()` method to find the index of a specific element in an array, and then checking if it's greater than 0 to verify its presence. **Pros and Cons** * **Includes()** + Pros: - More concise and readable code. - Can be faster since it doesn't require finding the exact index. + Cons: - May not work as expected for arrays with duplicate elements (only returns the first occurrence). - Requires modern JavaScript versions (ES6+) to use. * **IndexOf()** + Pros: - Works for arrays with duplicate elements, returning -1 if not found. - More reliable in older JavaScript versions (before ES6+). + Cons: - Longer code and potentially slower due to the extra operation. **Library Usage** Neither of these test cases uses a library explicitly. The `includes()` method is a built-in function in modern JavaScript, while `indexOf()` is also part of the standard library. **Special JS Features or Syntax** There are no special JavaScript features or syntax used in these benchmarking tests. **Alternative Approaches** Other alternatives for this type of benchmark could include: 1. Using `findIndex()` instead of `indexOf()`, which returns -1 if not found. 2. Comparing the performance of using `includes()` with other array methods, such as `some()` or `every()`. 3. Adding more elements to the arrays to test scalability and performance under load. Keep in mind that these alternative approaches would require modifications to the benchmark code and might introduce additional complexity.
Related benchmarks:
IndexOf vs Includes
JavaScript Benchmark: includes vs indexOf
IndexOf vs Includes in array
Array find with indexOf vs includes
find vs includes vs indexof
Comments
Confirm delete:
Do you really want to delete benchmark?