Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Sting prototype indexOf vs includes performance
(version: 1)
Comparing performance of:
indexOf vs includes
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
const string = "Quite a random string, which is not too long, but not too short neither";
Tests:
indexOf
!!(string.indexOf('str2') !== -1)
includes
string.includes('str2')
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:
3 months ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 18_7 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/26.2 Mobile/15E148 Safari/604.1
Browser/OS:
Mobile Safari 26 on iOS 18.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
indexOf
687527488.0 Ops/sec
includes
31214024.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The provided benchmark is designed to compare the performance of two different methods for searching for a substring ("str2") within a given string using JavaScript. Specifically, it tests the built-in `String.prototype.indexOf()` method against the more recent `String.prototype.includes()` method. ### Benchmark Details - **Benchmark Name**: "String prototype indexOf vs includes performance" - **Test Cases**: 1. **indexOf**: This test case uses `string.indexOf('str2') !== -1`, which checks if the substring 'str2' is present in the given string by returning the index of the substring if found. The comparison with -1 indicates that it checks for existence, as `indexOf` returns -1 when the substring is not found. 2. **includes**: This test case uses `string.includes('str2')`, which directly checks for the existence of the substring 'str2' in the given string, returning `true` if it exists and `false` if it does not. ### Pros and Cons #### `indexOf` - **Pros**: - Older method supported in virtually all JavaScript environments (including very old browsers). - Can be used to find the position of the substring, which is useful when the index is needed. - **Cons**: - Less readable and intuitive when solely checking for existence; the syntax involves an additional comparison (to -1). - Slightly less efficient for existence checks, as it requires additional operations. #### `includes` - **Pros**: - Introduced in ES6 (ECMAScript 2015), which enhances code readability with straightforward semantics. - Semantically expresses intention to check for existence, making the code cleaner and easier to understand. - Typically faster for simple existence checks due to the absence of an extra comparison step. - **Cons**: - Not available in very old environments (older than ES6). However, modern browsers have generally adopted this feature. ### Benchmark Results The results show the performance of each method, measured as the number of executions per second: - **includes**: 216,935,776.0 executions per second - **indexOf**: 206,587,184.0 executions per second Based on this data, `includes` performs slightly better than `indexOf` in the provided scenario. ### Conclusion and Considerations The benchmark is straightforward yet relevant, especially when considering how developers handle string searches in JavaScript. The results suggest that for modern development where browser compatibility is not a constraint, `includes` is preferable due to its performance and clarity. ### Alternatives Other methods to search for substrings in JavaScript include: - `RegExp` (Regular Expressions): This approach can be powerful when searching for patterns, but it comes with higher complexity and a performance cost. - Manual iteration: Using a loop or higher-order functions (like `Array.prototype.some()`) could achieve similar results, but this would generally be less efficient and more verbose compared to built-in methods. In summary, for substring existence checks in modern JavaScript, the `includes` method is typically the best choice, blending performance, readability, and ease of use.
Related benchmarks:
string indexOf and includes
String indexOf vs includes
IndexOf vs Includes str
JS string includes vs indexOf
Js Search -String IndexOf vs Includes
String.IndeOf vs. String.includes
indexOf vs includes search2
String.indexOf vs String.indexOf with the second parameter
.includes() vs indexOf() for single-character search in string
Comments
Confirm delete:
Do you really want to delete benchmark?