Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regex vs string.includes
(version: 1)
Comparing performance of:
Regex vs .includes
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
var doi = "10.1016/j.jtbi.2011.09.008"
Tests:
Regex
/\//.test(doi);
.includes
doi.includes("/")
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Regex
.includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Regex
76302136.0 Ops/sec
.includes
267198768.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the provided JSON is aimed at comparing two different string searching methods in JavaScript: using Regular Expressions (Regex) and using the String method `.includes()`. ### Options Compared 1. **Regex Method**: - **Test Case**: `/\\//.test(doi);` - **Description**: This method uses a regex pattern to test if the string `doi` contains a forward slash (`/`). The `test` method returns `true` or `false`, indicating the presence or absence of the pattern in the string. 2. **String.includes Method**: - **Test Case**: `doi.includes("/")` - **Description**: This approach uses the `includes()` method of the String prototype to check if the string `doi` includes the specific substring (`"/"`). It also returns a boolean value based on the presence of the substring. ### Performance Results According to the benchmark results: - The `.includes` method achieved **267,198,768 executions per second**. - The Regex method achieved **76,302,136 executions per second**. ### Pros and Cons of Approaches #### Regex Method - **Pros**: - Flexible and powerful for pattern matching. - Can be used for more complex string patterns beyond simple substring checks. - **Cons**: - Generally slower than simpler methods like `includes`, especially for straightforward checks. - Can be overkill when all that is needed is a simple existence check. #### String.includes Method - **Pros**: - Simplicity and readability make it easier to understand for developers, especially those who aren't well-versed in regular expressions. - Faster execution compared to regex for simple substring checks, as demonstrated in this benchmark. - **Cons**: - Less powerful than regex for complex pattern matching. - Limited to checking for existence of specific substrings without additional pattern-matching capabilities. ### Other Considerations - **Readability and Maintainability**: For cases where only a simple substring search is required, `.includes()` is preferred for its clarity. Using regex might confuse developers unfamiliar with pattern syntax. - **Browser Compatibility**: Both methods are widely supported in modern browsers, but it's always essential to verify compatibility when working with older versions or specific environments. ### Alternatives - **IndexOf**: Another alternative to both of these methods is using the method `.indexOf()`, which returns the index of the first occurrence of the specified substring, or `-1` if it is not found. This method can be less clear than `.includes()`, which directly conveys the intent of checking for existence. - **Regular Expressions with Different Flags**: While the benchmark tests a simple regex match, the regex can be further enhanced with flags (e.g., `g` for global matches), but this is only relevant in more complex scenarios. - **String Search Methods**: Depending on the complexity, string search libraries can be used, but in this case, both `.includes()` and regex are built-in methods that suffice. In summary, the benchmark highlights that for checking the presence of a substring, the simpler `.includes()` method is preferable in terms of performance and readability over regex, unless a more complex search pattern is required.
Related benchmarks:
RegEx.test vs. String.includes vs. String.match
RegEx.test vs. String.includes vs. String.match nested
RegEx.test vs String.includes
RegEx.test (with inline regex) vs. String.includes vs. String.match
Long regex test vs string includes
Longer regex test vs string includes
RegEx.test vs. String.includes vs. String.match pr
test regex vs regex vs includes
RegEx.test vs. String.includes vs. String.match db
Comments
Confirm delete:
Do you really want to delete benchmark?