Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS includes() vs regex test on simple string
(version: 1)
JS includes() vs regex test on simple string
Comparing performance of:
includes() vs regex test
Created:
11 months ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
/*your preparation JavaScript code goes here To execute async code during the script preparation, wrap it as function globalMeasureThatScriptPrepareFunction, example:*/ async function globalMeasureThatScriptPrepareFunction() { // This function is optional, feel free to remove it. // await someThing(); }
Tests:
includes()
'file.php'.includes('.php')
regex test
/\.php/.test('file.php')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes()
regex test
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
includes()
178547760.0 Ops/sec
regex test
50102312.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 11 months ago):
The provided benchmark evaluates the performance of two different methods for checking if a substring exists within a string in JavaScript: the `includes()` method and a regular expression test using the `test()` function. ### Tested Options 1. **`includes()` Method** - **Benchmark Definition:** `'file.php'.includes('.php')` - **Test Name:** `includes()` - **Purpose:** This method is used to determine whether a string contains a specified substring. It returns `true` if the substring is found and `false` otherwise. It is part of the ECMAScript 6 (ES6) specification. 2. **Regular Expression Test** - **Benchmark Definition:** `/\\.php/.test('file.php')` - **Test Name:** `regex test` - **Purpose:** This method uses a regular expression to search for the substring. The expression `/.php/` checks for the presence of ".php" in the string. The `test()` method returns `true` if the string matches the regex pattern and `false` otherwise. ### Performance Results The benchmark results show that the `includes()` method has a significantly higher execution rate: - **`includes()`:** 178,547,760 executions per second - **`regex test`:** 50,102,312 executions per second ### Pros and Cons #### `includes()` Method - **Pros:** - **Simplicity:** The syntax is straightforward, making the code easier to read and maintain. - **Performance:** As demonstrated in the benchmark, it performs far better than regular expressions for this specific use case. This aligns with the general expectation that simple string operations should outperform regex checks when the operation does not require complex pattern matching. - **Cons:** - **Limitations:** It can only check for direct substring matches and does not offer the flexibility that regex provides for more complex patterns. #### Regular Expression Test - **Pros:** - **Flexibility:** Regular expressions allow for complex pattern matching, including wildcards, character classes, and more elaborate searches beyond simple substring detection. - **Cons:** - **Performance:** As seen in the benchmark results, regex tests are slower compared to the `includes()` method for a simple substring match. - **Complexity:** Regex can be challenging to read and understand, especially for developers who are not familiar with regular expression syntax. ### Other Considerations When choosing between these two options, performance requirements and code readability/maintenance should be prioritized. For simple substring checks, `includes()` is the go-to method due to its speed and clarity. For more complicated pattern matching needs, regular expressions might be necessary despite the performance trade-off. ### Alternatives 1. **String.indexOf():** This is another alternative for checking if a substring exists in a string. The syntax is `'file.php'.indexOf('.php') !== -1`. It performs reasonably well but is not as clean as `includes()`. 2. **String.search():** This method uses a regex pattern (`'file.php'.search(/\.php/)`), but like the regex test, it can be slower than `includes()`, especially for simple matches. In summary, for checking if a specific substring is present in a string, using the `includes()` method is preferable for performance and readability, while regular expressions should be reserved for cases where more complex matching patterns are required.
Related benchmarks:
RegEx.test vs. String.includes vs. String.match
Regex check
RegEx.test vs. String.includes vs. String.match v2
test test
RegEx.test vs String.includes
RegEx.test vs. String.includes vs. String.match vs Array.includes
We test stuff!
new RegExp vs literal
String comp
Comments
Confirm delete:
Do you really want to delete benchmark?