Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array of strings, indexOf vs includes 2
(version: 0)
Comparing performance of:
includes vs indexOf
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
includes
const types = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] types.includes('e')
indexOf
const types = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'] types.indexOf('e')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes
indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Android 14; Mobile; rv:121.0) Gecko/121.0 Firefox/121.0
Browser/OS:
Firefox Mobile 121 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
36818152.0 Ops/sec
indexOf
37503652.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Overview** The benchmark is designed to compare the performance of two JavaScript methods: `indexOf` and `includes`. These methods are used to search for a specific value within an array. **Options Compared** Two options are compared: 1. **`indexOf`**: This method returns the index of the first occurrence of the specified value in the array. If the value is not found, it returns -1. 2. **`includes`**: This method returns `true` if the specified value is present in the array, and `false` otherwise. **Pros and Cons** * **`indexOf`**: * Pros: Can be more efficient when searching for a specific index or when working with arrays where indexing is necessary. * Cons: May not be as straightforward to use when searching for a value without knowing its position, as it returns an index instead of a boolean result. It also has a higher time complexity (O(n)) compared to `includes`. * **`includes`**: * Pros: More concise and expressive when searching for a value in an array. It's also more intuitive to use, especially when working with larger arrays. * Cons: May be slower than `indexOf` due to its implementation and the fact that it needs to check every element in the array. **Library Usage** In this benchmark, there is no explicit library usage mentioned. However, both methods are part of the ECMAScript standard, which means they're inherently supported by JavaScript engines without any additional libraries or imports required. **Special JS Feature/Syntax** There's an example of using `const` for variable declaration and initialization in the scripts. `const` is a keyword that declares variables that can be reassigned only once. It's used here to declare the `types` array and initialize it with values. **Other Alternatives** If you need to compare other methods or approaches, some alternatives could include: * Using different data structures (e.g., linked lists or trees) instead of arrays. * Implementing custom search algorithms for finding specific values in arrays. * Comparing performance using `forEach`, `filter()`, or other array methods. Keep in mind that the choice of approach depends on the specific requirements and constraints of your project.
Related benchmarks:
IndexOf vs Includes
String in Array - includes() vs indexOf() vs test()
Array find with indexOf vs includes
String.indexOf vs Array split and includes
find vs includes vs indexof
Comments
Confirm delete:
Do you really want to delete benchmark?