Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegEx.test vs. String.includes vs. String.match Non matching
(version: 1)
Comparing performance of:
RegEx.test vs String.includes vs String.match
Created:
8 months ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = "Hello world!"; var regex = /Foo/;
Tests:
RegEx.test
regex.test(string);
String.includes
string.includes("Foo");
String.match
string.match("Foo");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
RegEx.test
String.includes
String.match
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36
Browser/OS:
Chrome 140 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
RegEx.test
76855072.0 Ops/sec
String.includes
253770048.0 Ops/sec
String.match
14551715.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 8 months ago):
The benchmark defined in the provided JSON compares the performance of three different methods for checking if a specific substring exists within a string in JavaScript. The methods being tested are: 1. **RegEx.test** 2. **String.includes** 3. **String.match** ### Description of Each Method: 1. **RegEx.test**: - **Definition**: This method uses a regular expression to check for a match in a string. - **Usage**: The regex `/Foo/` is invoked with the `test` method on a string (`"Hello world!"`) to check if the substring "Foo" exists. - **Pros**: - Flexibility: Regular expressions can be used for complex pattern matching beyond simple substring detection. - Power: Can match various patterns (e.g., character classes, quantifiers). - **Cons**: - Performance: RegEx operations can be slower for simple substring searches compared to string methods, especially since it involves pattern parsing. - Complexity: Regular expressions can be harder to read and maintain. 2. **String.includes**: - **Definition**: This method returns `true` or `false` indicating whether the string contains the specified substring. - **Usage**: Invoked with the string to check if it contains the substring "Foo". - **Pros**: - Simplicity: Easy to understand and use for straightforward substring searches. - Readability: Clear intent—checking for the presence of a substring. - **Cons**: - Limited to substring checks; cannot perform more complex matching that regex can handle. 3. **String.match**: - **Definition**: This method retrieves the matches of a string based on a regex or string search. - **Usage**: It applies the regex to the string but returns an array of matches (or `null` if no match is found). - **Pros**: - Useful for extracting matches, not just checking for existence. - Can work with regex for advanced matching capabilities. - **Cons**: - If used just for existence checks, it may be less performant compared to `includes` since it involves creating a match array. - More complex to understand as it returns arrays or `null`. ### Benchmark Results Analysis: Based on the latest benchmark results provided: - **String.includes** performed the best with **253,770,048 executions per second**, suggesting it's the most efficient for simple substring searches. - **RegEx.test** had a significantly lower performance at **76,855,072 executions per second**, indicating that while it offers flexibility for complex patterns, it is less performant for basic checks. - **String.match** was the slowest at **14,551,715 executions per second**, which aligns with the observation that using it just for existence checks is less efficient. ### Other Considerations: - **Use Case**: The selection between these methods depends on the use case; simple substring checks are best suited to `includes`, while `RegEx` is better for complex pattern matching. - **Browser Performance**: Variations in performance might occur across different browsers or environments. The results here were specific to Chrome 140 on a Windows desktop. - **Alternatives**: Other methods, such as using the `indexOf` method, could also be considered, which also performs substring checks but has a slightly different syntax and semantics. By understanding the performance characteristics and use cases of these different string-checking methods, software engineers can make informed decisions about which approach to use in their applications, optimizing for performance and clarity.
Related benchmarks:
RegEx.test vs. String.includes vs. String.match
RegEx.test vs. String.includes vs. String.match insensitive
RegEx.test vs. String.includes vs. String.match v2
RegEx.test vs String.includes
RegEx.test vs. String.includes vs. String.match (multiple words in regex)
RegEx.test vs. String.includes vs. String.match1
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 x 2
Comments
Confirm delete:
Do you really want to delete benchmark?