Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
IndexOf vs Includes Check in string
(version: 1)
Banana
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') !== -1
Includes
string.includes('tempor')
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:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
IndexOf
121484672.0 Ops/sec
Includes
131672744.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
In the provided benchmark definition, two methods for checking the presence of a substring in a string in JavaScript are compared: `indexOf` and `includes`. ### Options Compared 1. **indexOf** - **Test Case:** `string.indexOf('tempor') !== -1` - **Description:** The `indexOf` method is used to determine the index position of the substring 'tempor' within the larger string. If the substring is found, it returns the index (0 or greater); if not found, it returns -1. The comparison to -1 checks for the presence of the substring. 2. **includes** - **Test Case:** `string.includes('tempor')` - **Description:** The `includes` method is a more modern and straightforward way to check for the presence of a substring. It returns true if the substring is found in the string and false otherwise. ### Pros and Cons #### indexOf - **Pros:** - Supported in all JavaScript environments, dating back to older versions, making it highly compatible. - Provides the ability to know the position of the substring, which may be useful in certain scenarios where the index is needed. - **Cons:** - Syntax can be considered less readable than `includes`, as the logic for presence check requires an additional comparison (`!== -1`). - Slightly more cumbersome for simple presence checks, as it is more verbose. #### includes - **Pros:** - Syntax is simpler and more intuitive, improving code readability. A straightforward boolean result is returned. - Specifically designed for checking the presence of a substring, making it semantically clearer. - **Cons:** - Not supported in very old browsers (typically before Internet Explorer 11), which might restrict its use in legacy applications. ### Benchmark Results The benchmark results show that the `includes` method outperforms the `indexOf` method in terms of execution speed, with 131,672,744 executions per second compared to 121,484,672 executions per second for `indexOf`. This suggests that `includes` is not only more readable but also more efficient in this test scenario. ### Other Considerations 1. **Performance Context:** While the benchmark indicates `includes` is faster in this particular case, performance can vary depending on the string's size and the runtime environment. In some contexts, `indexOf` might be optimized differently, so testing in the specific use case is advisable. 2. **Legacy Support:** If supporting older browsers is a priority, `indexOf` might be the better choice, while transpilation tools (like Babel) could be used to bring `includes` functionality to older environments. ### Alternatives - **Regular Expressions:** For complex matching patterns, regular expressions can be utilized (e.g., `/tempor/.test(string)`), which might provide powerful pattern matching at the cost of performance. - **Other String Methods:** For specific cases like searching for whole words, different methods or combinations of methods might be more appropriate. In summary, this benchmark compares two methods of substring searching in JavaScript, with implications for performance, readability, and compatibility, important factors that software engineers must consider when choosing between them.
Related benchmarks:
IndexOf vs Includes in string
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 vs Includes in string as boolean
IndexOf vs Includes in string 2
Comments
Confirm delete:
Do you really want to delete benchmark?