Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
IndexOf vs Includes for a pathname string with duplicated slashes
(version: 1)
This is a test to see which method is faster for finding a double slash in a typical pathname.
Comparing performance of:
IndexOf vs Includes
Created:
11 months ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var dublicatedSlahesPathname = '/measurethat//net/Benchmarks//Test/Me/Baby' var dublicatedTrailingSlahesPathname = '/measurethat//net/Benchmarks//Test/Me/Baby/' var normalizedPathname = '/measurethat/net/Benchmarks/Test/Me/Baby' var normalizedTrailingPathname = '/measurethat/net/Benchmarks/Test/Me/Baby/'
Tests:
IndexOf
dublicatedSlahesPathname.indexOf('//') normalizedPathname.indexOf('//')
Includes
dublicatedSlahesPathname.includes('//') normalizedPathname.includes('//')
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:
11 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
IndexOf
269181536.0 Ops/sec
Includes
275122432.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 11 months ago):
The benchmark tests the performance of two different methods for finding a double slash (`'//'`) in a pathname string: `indexOf()` and `includes()`. Both methods are commonly used in JavaScript to search for substrings within a string but differ in their approach and use cases. ### Methods Tested 1. **IndexOf** - **Description**: The `indexOf()` method searches for a specified substring within a string and returns the index at which it first occurs, or `-1` if the substring is not found. This method has been part of JavaScript from its early versions. - **Usage in Test**: The benchmark invokes `indexOf('//')` on two pathnames: `dublicatedSlahesPathname` (which contains double slashes) and `normalizedPathname` (which does not). 2. **Includes** - **Description**: The `includes()` method tests whether a specified substring exists within the string, returning `true` or `false`. It is more readable and expresses intent more clearly than `indexOf()`, especially for checking presence. - **Usage in Test**: Similarly, `includes('//')` is tested on both pathnames. ### Pros and Cons - **IndexOf** - **Pros**: - Returns the index of the substring, which can be useful if the position matters. - Supported in all JavaScript environments since early versions. - **Cons**: - Less readable when used for simple presence checks—returns `-1` rather than a boolean value. - Slightly more tedious to check presence (`indexOf` === -1). - **Includes** - **Pros**: - More descriptive and easier to read; it clearly indicates you are checking for presence. - Returns a boolean, making it more straightforward for conditional checks. - **Cons**: - Not as widely supported in very old JavaScript environments (though this is largely mitigated in modern development). ### Benchmark Results The benchmark results indicate the performance of each method, measured by the number of executions per second: - **Includes**: 275,122,432 executions per second. - **IndexOf**: 269,181,536 executions per second. From the results, it is evident that `includes()` performed better than `indexOf()` in this specific test case. ### Other Considerations - **Performance Context**: The performance difference between `indexOf()` and `includes()` may vary based on the complexity of the input strings, their length, and the execution environment. While one method might be faster for certain cases, the general readability and intent clarity of `includes()` make it preferable in many situations involving mere existence checks. - **Alternative Methods**: - In addition to `indexOf()` and `includes()`, other string search methods exist: - **Regular Expressions**: Can provide powerful search and match capabilities but often at a cost of performance and complexity. - **Custom Implementations**: Developers can create their own substring search functions, which may be optimized for specific use cases but generally are not worth the effort unless there are unique scenarios. Choosing the right method depends not just on performance but also on the clarity of intent and maintenance of the code. In scenarios where the presence of a substring is what is needed, `includes()` is typically the better option due to its increased readability and straightforwardness. In conclusion, while both methods serve their purpose well, benchmarks such as this one can help guide developers toward the best choice for their specific use cases based on performance metrics and code maintainability.
Related benchmarks:
Slice/join vs Substr
IndexOf vs regex
Split vs Include
search string by include vs index of
indexOf vs. match
startsWith vs indexOf vs endsWith
endswith vs indexof
js.indexof.speed.text
indexOf vs search
Comments
Confirm delete:
Do you really want to delete benchmark?