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}$/
regex.test(string);
regex2.test(string);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
RegEx.test | |
RegEx2.test |
Test name | Executions per second |
---|---|
RegEx.test | 6589758.5 Ops/sec |
RegEx2.test | 6103125.0 Ops/sec |
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:
regex.test(string);
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:
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.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
.test()
): Pros:^
and $
)..test(string)
with no anchors): Pros: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:
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.