Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
[STRING] IndexOf vs Includes
(version: 1)
simple sample to test perf
Comparing performance of:
IndexOf vs Includes
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.'
Tests:
IndexOf
string.indexOf('tempor') >= 0 string.indexOf('123') >= 0
Includes
string.includes('tempor') string.includes('123')
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:
4 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:146.0) Gecko/20100101 Firefox/146.0
Browser/OS:
Firefox 146 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
IndexOf
1569946240.0 Ops/sec
Includes
1570696704.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark you provided evaluates two JavaScript methods for checking the presence of substrings within a larger string: `indexOf` and `includes`. Each method is tested for its performance when searching for a substring. Here’s an explanation of what is being compared, along with the pros and cons of each approach: ### Benchmark Overview 1. **Benchmark Name**: "[STRING] IndexOf vs Includes" 2. **Benchmark Description**: This is a simple performance test to measure and compare the execution speed of `String.indexOf` and `String.includes`. ### Options Compared - **`String.indexOf()`**: - **Benchmark Definition**: `string.indexOf('tempor') >= 0` and `string.indexOf('123') >= 0` - This method returns the position of the first occurrence of the specified substring. If the substring is not found, it returns -1. - **`String.includes()`**: - **Benchmark Definition**: `string.includes('tempor')` and `string.includes('123')` - This method returns a boolean value indicating whether the specified substring exists within the string. ### Pros and Cons #### `String.indexOf()` - **Pros**: - Compatibility: Supported in all browsers, including older ones (introduced in ECMAScript 3). - Versatile: Can be used for more than just presence checks since it returns the position of the substring. - **Cons**: - More verbose for presence checks: Requires a comparison (`>= 0`) to determine if the substring exists. - Potentially less readable for simply checking for presence since users need to interpret the index result. #### `String.includes()` - **Pros**: - Simplicity: The syntax is straightforward for checking substring presence, returning a boolean directly. - Readability: More intuitive for developers when the intention is only to check for the existence of a substring. - **Cons**: - Browser Support: While widely supported in modern browsers (introduced in ECMAScript 6), it may not be available in very old browsers. - Less versatile: Does not return the index, which limits its utility if the location of the substring is needed. ### Benchmark Results According to the benchmark results provided: - The `includes` method can execute approximately **1,800,419,584 times per second**. - The `indexOf` method has an execution speed of about **1,527,778,304 times per second**. This indicates that the `includes` method is faster than `indexOf` in this test scenario, demonstrating its efficiency for the specific use case of substring presence checking. ### Other Considerations 1. **Performance Variability**: The performance of these methods may vary depending on the string length, substring length, and the content being searched. This benchmark gives a ballpark figure but specific use cases may yield different results. 2. **Alternatives**: While `indexOf` and `includes` are the primary methods used for checking substring presence, other alternatives include: - Regular expressions (using `RegExp.test`), which can handle more complex search patterns but come with higher overhead in terms of performance. - Third-party libraries for string manipulation (like Lodash) which provide utility functions but add additional dependencies to your project. In conclusion, when evaluating which method to use for checking the presence of a substring, `includes` is preferable for clarity and performance in modern JavaScript environments, while `indexOf` remains valuable for compatibility and more complex operations.
Related benchmarks:
IndexOf vs Includes in string 233
IndexOf vs Includes in string 333
IndexOf === -1 vs Includes in string
IndexOf vs Includes in string tedt
IndexOf vs Includes in string with check
IndexOf > -1 vs Includes in string
indexOf >-1 vs includes
IndexOf with equals vs >
IndexOf vs Includes in string 2
Comments
Confirm delete:
Do you really want to delete benchmark?