Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegEx.test vs. String.includes vs. String.match WITH regex!
(version: 1)
Comparing performance of:
RegEx.test vs String.match
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = "Hello world!"; var regex = /Hello/;
Tests:
RegEx.test
regex.test(string);
String.match
string.match(regex);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
RegEx.test
String.match
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; rv:134.0) Gecko/20100101 Firefox/134.0
Browser/OS:
Firefox 134 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
RegEx.test
40461080.0 Ops/sec
String.match
23607358.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark defined in the provided JSON tests the performance of two different approaches for checking if a substring matches a pattern in a string using regular expressions in JavaScript. The two approaches being compared are: 1. **`RegExp.test` Method**: - **Usage**: This method executes a search for a match between a regular expression and a specified string. It returns `true` or `false` depending on whether the match is found. - **Benchmark Definition**: `regex.test(string);` - **Pros**: - Simple and straightforward for checking the existence of a match. - Generally more efficient for mere presence checks, returning a boolean value without the overhead of returning details of matches. - **Cons**: - Limited to a simple existence check and does not provide information about the match (like the matched substring or its position) which may require an additional operation if those details are needed. 2. **`String.match` Method**: - **Usage**: This method retrieves the matches when matching a string against a regular expression. It returns an array containing the matches or `null` if no match is found. - **Benchmark Definition**: `string.match(regex);` - **Pros**: - Provides more detailed information about the match, including the actual match content and position if the result is available. - **Cons**: - More computationally expensive compared to `RegExp.test`, especially when you only need to check for the existence of a match, as it returns an array which may involve additional memory overhead. ### Other Considerations - When considering performance, the results show a clear advantage for the `RegExp.test` method, which achieved approximately 40.5 million executions per second, compared to about 23.6 million for `String.match`. If the requirement is solely to check for presence, `RegExp.test` is preferable due to its faster execution time. - However, if more detail about the matches is needed later on, `String.match` may be worth the performance cost. ### Alternatives - **`String.includes`**: Although not used in this benchmark, another popular method for substring checks is `String.includes` which checks if a string contains a certain substring. It is generally faster than regular expressions for simple checks but does not support pattern matching offered by regex. It wouldn't work for complex patterns that regex can handle. - **Other Regex Methods**: Depending on the use case, other regex methods such as `String.replace` or `String.search` might be applicable but would depend on the specific needs for text processing. In summary, the benchmark highlights the performance differences between `RegExp.test` and `String.match`—the former being faster for existence checks while the latter offers greater detail at the cost of performance. Depending on the specific requirements of the application, developers can choose the appropriate method considering both speed and functionality.
Related benchmarks:
RegEx.test vs. String.includes vs. String.match
RegEx.test vs. String.includes vs. String.indexOf
RegEx.test vs. String.includes vs. String.match insensitive
RegEx.test vs. String.includes vs. String.match v2
RegEx.test vs RegEx.match when fails
RegEx.test vs String.includes
RegEx.test vs. String.includes vs. String.match(Regex)
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?