var REGEX_URL="(?:[^a-z])";
"http://www.google.com?q=Georgi+Facello".match(REGEX_URL)
"zwexcvbfytuhijgfvyghjvndveyusbhfdcjnmgbycfhjnmtberuncimv2345678bimfrvekmocdjnxhhbhebvhdmbfxvhebmfvhbxvmehbvnxmehfmvjehxbvjehmbxehbvhvhrvnm".match(REGEX_URL)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
url | |
long |
Test name | Executions per second |
---|---|
url | 3585012.8 Ops/sec |
long | 2915709.2 Ops/sec |
I'd be happy to explain the benchmark being tested on MeasureThat.net.
The benchmark being tested is a JavaScript microbenchmark that measures the performance of regular expression matching in two different scenarios: url
and long
.
Script Preparation Code
The script preparation code provided is var REGEX_URL = "(?:[^a-z])";
. This line defines a regular expression pattern using the non-capturing group syntax ((?:
) and a negated character class ([^a-z]
). The (?:)
syntax allows us to define a pattern without creating a capture group, which can improve performance. The [^a-z]
character class matches any character that is not a lowercase letter.
Html Preparation Code The html preparation code is empty in this case.
Benchmark Definition The benchmark definition provides two test cases:
"http://www.google.com?q=Georgi+Facello".match(REGEX_URL)
. The pattern REGEX_URL
matches any character that is not a lowercase letter, so this test case checks how fast the JavaScript engine can execute a simple regular expression match."zwexcvbfytuhijgfvyghjvndveyusbhfdcjnmgbycfhjnmtberuncimv2345678bimfrvekmocdjnxhhbhebvhdmbfxvhebmfvhbxvmehbvnxmehfmvjehxbvjehmbxehbvhvhrvnm".match(REGEX_URL)
. This test case checks how fast the JavaScript engine can execute a longer regular expression match.Library There is no explicit library mentioned in this benchmark. The regular expression pattern is defined directly in the script preparation code.
Special JS feature or syntax
The non-capturing group syntax (?:)
and the negated character class [^a-z]
are standard JavaScript features that allow us to define complex regular expressions efficiently.
Pros and Cons of different approaches
(?:
) can improve performance by avoiding unnecessary capture groups.[^a-z]
) is often faster than using the ^
character to negate an entire character class.url
test case) tends to be faster than using more complex patterns.However, these optimizations may not always result in significant performance gains, and the impact of each optimization technique can vary depending on the specific use case and implementation details.
Other alternatives
Some alternative approaches to benchmarking regular expression performance include:
RegExp.test()
, RegExp.exec()
)In this case, MeasureThat.net's approach seems suitable for testing regular expression matching performance in simple scenarios like these.