Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegEx.test2 vs. String.includes vs. String.match
(version: 0)
Comparing performance of:
RegEx.test vs RegEx2.test
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = "ungigel.altgigel@vodafone.com"; var regex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/ var regex2 = /^\w+([\.-]?[a-zA-Z0-9])+@([\.-]?[a-zA-Z0-9])+\.[a-zA-Z]{2,3}$/
Tests:
RegEx.test
regex.test(string);
RegEx2.test
regex2.test(string);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
RegEx.test
RegEx2.test
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
RegEx.test
6589758.5 Ops/sec
RegEx2.test
6103125.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain what's being tested in this benchmark. **What is being tested?** The provided JSON represents two JavaScript microbenchmarks that compare the performance of regular expression (regex) matching using different approaches: 1. `regex.test(string);` 2. `regex2.test(string);` These benchmarks test the speed of executing a regex pattern on a fixed input string, which contains an email address. **Comparison options** The benchmark compares two approaches: 1. **`regex.test(string);`**: This uses a traditional regex implementation, where the `^` and `$` anchors are included in the pattern. This is a common approach to matching strings from the beginning and end. 2. **`regex2.test(string);`**: This uses a modified regex implementation that removes the `^` and `$` anchors from the pattern. Instead, it relies on the fact that the input string starts with a word character (`\w+`) followed by an optional separator (`[\\.-]?`), then another word character or separator (`[\\.-]?\\w+)*`, and finally ends with a dot (`.`) and two to three more word characters (`[\\.-]?\\w+`). This approach is designed to avoid the overhead of anchors. **Pros and cons** * **Traditional regex implementation (`.test()`)**: Pros: + Easy to understand and implement. + Works well for most use cases. * Cons: + Can be slower due to the overhead of anchors (`^` and `$`). + May not be optimal for very long strings or complex patterns. * **Modified regex implementation (`.test(string)` with no anchors)**: Pros: + Faster execution time, especially for longer strings or complex patterns. + More efficient use of resources. * Cons: + Can lead to unexpected behavior if the input string is not as expected. + May require more careful handling of edge cases. **Library and purpose** Neither `regex.test(string)` nor `regex2.test(string)` uses a specific JavaScript library. These are built-in methods provided by the JavaScript engine (V8 in this case) for executing regex patterns. However, if we consider the modified regex implementation (`regex2.test(string)`), it's worth noting that some libraries (e.g., `jsregex` or `regex-try`) might implement similar optimizations or alternatives to traditional regex matching. But in this specific benchmark, the implementation is custom and not using a third-party library. **Special JavaScript feature or syntax** There are no special JavaScript features or syntax used in these benchmarks. The only notable aspect is the use of raw strings (`var string = "..."`) to define the input strings, which helps avoid issues with escaped characters or Unicode normalization. **Alternatives** Other alternatives for testing regex performance might include: 1. **Benchmarking libraries**: Tools like Benchmark.js, Regexp-benchmark, or jsperf can provide more comprehensive and flexible benchmarking capabilities. 2. **Custom implementations**: Writing custom JavaScript code to test different regex patterns and comparisons can offer more control over the benchmarking process. 3. **Third-party libraries**: Some libraries (e.g., `jsregex` or `regex-try`) might provide optimized regex matching implementations that could be used for benchmarks. Overall, this benchmark provides a simple yet informative way to compare the performance of traditional vs. modified regex matching approaches.
Related benchmarks:
String.match vs. RegEx.test
String.match vs. RegEx.test1
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?