Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegEx.test vs. String.includes vs. String.match (with multiple strings)
(version: 0)
Comparing performance of:
RegEx.test vs Iteration and String.includes vs String.match
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
const string = 'Outstanding'; const regex = /toda|bfi|coms|cot|cos|ipe|ips|rn|rm|ap|sf|st|sp|ss|sv|to|t|o|e|s/; const searchStrings = ['toda', 'bfi', 'coms', 'cot', 'cos', 'ipe', 'ips', 'rn', 'rm', 'ap', 'sf', 'st', 'sp', 'ss', 'sv', 'to', 't', 'o', 'e', 's'];
Tests:
RegEx.test
/toda|bfi|coms|cot|cos|ipe|ips|rn|rm|ap|sf|st|sp|ss|sv|to|t|o|e|s/.test('Outstanding');
Iteration and String.includes
['toda', 'bfi', 'coms', 'cot', 'cos', 'ipe', 'ips', 'rn', 'rm', 'ap', 'sf', 'st', 'sp', 'ss', 'sv', 'to', 't', 'o', 'e', 's'].some(s => 'Outstanding'.includes(s));
String.match
'Outstanding'.match(/toda|bfi|coms|cot|cos|ipe|ips|rn|rm|ap|sf|st|sp|ss|sv|to|t|o|e|s/);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
RegEx.test
Iteration and String.includes
String.match
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36
Browser/OS:
Chrome 144 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
RegEx.test
47026004.0 Ops/sec
Iteration and String.includes
7932545.5 Ops/sec
String.match
31970310.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. **Overview** The benchmark compares three different approaches to test if a string contains specific substrings: 1. `RegEx.test()`: using a regular expression with the `test()` method 2. `String.includes()`: using the `includes()` method 3. `String.match()`: using the `match()` method with a regular expression **Options Compared** The three options are compared in terms of execution speed, which is measured by the number of executions per second. **Pros and Cons of Each Approach:** 1. **RegEx.test():** * Pros: + Fastest execution time due to native engine support + Can be more flexible with regex patterns * Cons: + Less readable and maintainable code due to the complexity of regex patterns + May throw errors if the pattern is invalid or too complex 2. **String.includes():** * Pros: + More readable and maintainable code, as it's a simple string comparison + Throwing an error is explicit and easy to handle * Cons: + Slower execution time compared to `RegEx.test()` 3. **String.match():** * Pros: + Fast execution time due to native engine support for regex patterns + Can be more flexible with regex patterns, similar to `RegEx.test()` * Cons: + Returns an array of matches or null if no match is found, which can lead to unexpected behavior + May throw errors if the pattern is invalid or too complex **Library Used:** None explicitly mentioned in this benchmark. **Special JS Feature/Syntax:** The `some()` method is used in one of the test cases. This method returns a boolean value indicating whether at least one element in an array passes the test implemented by its provided function. **Other Considerations:** * The benchmark assumes that the input string "Outstanding" contains all the substrings specified in the regex pattern. * It's worth noting that the `RegEx.test()` method is generally faster than using `String.includes()` or `String.match()`, but the difference may not be significant for short strings. **Alternatives:** If you need to compare these approaches, here are some alternatives: * Use a JavaScript testing library like Jest or Mocha to create more complex benchmarks. * Compare performance using other methods, such as measuring the execution time of each function in isolation or using microbenchmarking tools. * Experiment with different regex patterns, string manipulations, and optimization techniques to find the most efficient approach for your specific use case. Keep in mind that the choice of method depends on your specific requirements, personal preference, and familiarity with JavaScript.
Related benchmarks:
Regex vs startsWith
includes vs regex
RegEx.matchAll vs includes with match
RegExp.test() vs Array.includes()
Comments
Confirm delete:
Do you really want to delete benchmark?