Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegEx.test vs String.includes (mutiple pattern)
(version: 1)
Comparing performance of:
RegEx.test vs String.includes
Created:
6 months ago
by:
Guest
Jump to the latest result
Script Preparation code:
const string = 'framed canvas'; const r = /framed|canvas/; const p = ['framed', 'canvas'];
Tests:
RegEx.test
r.test(string)
String.includes
p.some(s => string.includes(s))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
RegEx.test
String.includes
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36 Edg/141.0.0.0
Browser/OS:
Chrome 141 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
RegEx.test
66643288.0 Ops/sec
String.includes
63051112.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 6 months ago):
The benchmark defined in the provided JSON explores the performance of two different approaches for matching substrings within a string: using regular expressions (`RegEx.test`) and using the `String.includes` method. Here’s a breakdown of the test and the alternatives considered: ### Test Options 1. **RegEx.test** - **Benchmark Definition**: `r.test(string)` checks if either "framed" or "canvas" exists within the string "framed canvas". - **Pros**: - Can match multiple patterns with a single expression, making it concise for complex scenarios. - Powerful for advanced pattern-matching capabilities, including more than just simple string matches (such as using wildcards or other regex functionalities). - **Cons**: - Can have a steeper learning curve due to its syntax and complexity. - Regex performance can be variable based on pattern complexity and the input size; certain patterns might lead to slower performance due to backtracking. 2. **String.includes** - **Benchmark Definition**: `p.some(s => string.includes(s))` iteratively checks if "framed" or "canvas" is included in "framed canvas" using an array of substrings. - **Pros**: - Intuitive and straightforward syntax, making it easier to read and maintain. - Generally performs well for simple substring matches, especially when searching for a limited number of hard-coded keywords. - **Cons**: - Less efficient for matching multiple strings, as it requires multiple checks against the target string. - Lacks the advanced pattern-matching capabilities of regular expressions; you can only search for exact substrings. ### Performance Results According to the latest benchmark results: - **RegEx.test** achieved approximately **66,643,288** executions per second. - **String.includes** managed about **63,051,112** executions per second. From these results, `RegEx.test` demonstrated a slight performance advantage over `String.includes` in this specific test on the specified browser and device context. ### Other Considerations - **Use Case Complexity**: The choice between `RegEx.test` and `String.includes` heavily depends on the complexity of the use case. For simple checks where only exact string matches are required, `String.includes` is clearly preferable. However, for more complex patterns, regex is necessary. - **Regex Overhead**: It’s important to consider that regex may incur overhead in terms of debugging and testing due to its syntactical complexity. - **Browser Performance**: Benchmark results can vary significantly based on the runtime environment (e.g., the specific JavaScript engine in use, such as V8 in Chrome). Performance should be tested in the environments relevant to the application. ### Alternatives - **String.indexOf**: An older method to check for the existence of a substring, returning the index of the first occurrence or -1 if not found. This method is being used less frequently since `String.includes` is more readable. - **String.match**: Can also utilize regex to return matches in an array, offering further flexibility. - **Using Libraries**: For more advanced string manipulation and pattern-matching, libraries such as Lodash or Underscore can provide additional utility functions, although for simple checks like these, native methods are preferred. In summary, select the method based on clarity and your specific requirements: if ease of use and simplicity are paramount, go for `String.includes`. If you need the power of complex pattern matching, `RegEx.test` is your tool of choice.
Related benchmarks:
RegEx.test vs. String.includes vs. String.match
RegEx.test vs. String.includes vs. String.match insensitive
RegEx.test vs String.includes
RegEx.test vs. String.includes incasesensitive
RegEx.test vs. String.includes vs. String.match(Regex)
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 3
Comments
Confirm delete:
Do you really want to delete benchmark?