Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegEx.test vs. String.includes vs. String.match chitza
(version: 0)
Comparing performance of:
RegEx.test vs String.includes vs String.match
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = " - template: ./test/run-athena.yaml"; var regex = new RegExp('(?:- template:)(.*)$', 'gm');
Tests:
RegEx.test
regex.test(string);
String.includes
string.includes("- template:");
String.match
string.match("- template:");
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:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:135.0) Gecko/20100101 Firefox/135.0
Browser/OS:
Firefox 135 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
RegEx.test
45499224.0 Ops/sec
String.includes
1506075520.0 Ops/sec
String.match
14259175.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided JSON represents a JavaScript microbenchmark test case on the MeasureThat.net website. The benchmark compares three different approaches for testing regular expressions: `RegEx.test`, `String.includes`, and `String.match`. These methods are used to extract a specific pattern from a given string. **Approaches Compared** 1. **`RegEx.test()`**: This method uses the `RegExp.test()` method, which attempts to match the regular expression against the entire input string. 2. **`String.includes()`**: This method uses the `includes()` method of the String prototype, which returns `true` if the string contains the specified value (in this case, the regular expression pattern). 3. **`String.match()`**: This method uses the `match()` method of the String prototype, which returns an array containing all matches of the regular expression in the entire input string. **Pros and Cons** * **`RegEx.test()`**: + Pros: Can be more efficient for small patterns or when exact matching is required. + Cons: May be slower than other methods for larger patterns or when using complex regular expressions. * **`String.includes()`**: + Pros: Often faster than `RegExp.test()` and can handle large strings efficiently. However, it may not return a match if the pattern is not found at the beginning of the string. + Cons: May be slower for very small patterns or when exact matching is required. * **`String.match()`**: + Pros: Can be faster than `RegExp.test()` and can handle multiple matches. However, it returns an array, even if no match is found. + Cons: May be slower for very small patterns or when exact matching is required. **Libraries and Special Features** In this benchmark, none of the methods use a specific library. However, `String.includes()` uses the JavaScript method to check if a string contains a substring. This is implemented in the browser's engine, not a separate library. No special JavaScript features are used in this benchmark. **Alternative Approaches** Other approaches for testing regular expressions include: 1. **`RegExp.exec()`**: Similar to `RegExp.test()`, but returns an array of matches instead of a boolean value. 2. **`String.indexOf()`**: Returns the index of the first occurrence of the specified value, or -1 if not found. 3. **`String.replace()`**: Can be used to test for specific patterns by checking if the string remains unchanged after replacement. These alternative approaches may have different performance characteristics and use cases compared to the methods tested in this benchmark.
Related benchmarks:
RegEx.test vs String.includes
Long regex test vs string includes
Longer regex test vs string includes
RegEx.test vs. String.includes vs. String.match db
Comments
Confirm delete:
Do you really want to delete benchmark?