var REGEX_1 = /"{0,1}cpf"{0,1}[:=]"{0,1}([\w\s]{1,})/;
var REGEX_2 = /"{0,1}cpf"{0,1}[:=]\"{0,1}([\w\s]+)/;
var REGEX_3 = /"{0,1}cpf"{0,1}[:=]"{0,1}([^ ,]*)/;
REGEX_1.test("cpf=12,cpf=12,cpf=12,cpf=12,cpf=12,cpf=12,cpf=12,cpf=12,cpf=12,cpf=12,cpf=12,cpf=12")
REGEX_2.test("cpf=12,cpf=12,cpf=12,cpf=12,cpf=12,cpf=12,cpf=12,cpf=12,cpf=12,cpf=12,cpf=12,cpf=12")
REGEX_2.test("cpf=12,cpf=12,cpf=12,cpf=12,cpf=12,cpf=12,cpf=12,cpf=12,cpf=12,cpf=12,cpf=12,cpf=12")
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
REGEX_1 | |
REGEX_2 | |
REGEX_3 |
Test name | Executions per second |
---|---|
REGEX_1 | 9199638.0 Ops/sec |
REGEX_2 | 6628388.5 Ops/sec |
REGEX_3 | 5650891.5 Ops/sec |
Benchmark Explanation
The provided benchmark is designed to test the performance of regular expression (regex) patterns in JavaScript. The goal is to compare the execution speed of three different regex patterns: REGEX_1
, REGEX_2
, and REGEX_3
.
Regex Patterns
REGEX_1
:var REGEX_1 = /"{0,1}cpf"{0,1}[=:]"{0,1}([\\w\\s]{1,})/;
This regex pattern matches the string "cpf" with optional surrounding quotes and an optional colon and equals sign ([:=]
). The parentheses around [^ ,]*
capture one or more word characters (letters, numbers, or underscores) after the colon.
REGEX_2
:var REGEX_2 = /"{0,1}cpf"{0,1}[=:]\\\"{0,1}([\\w\\s]+)/;
This regex pattern is similar to REGEX_1
, but with a backslash (\
) before the colon and equals sign ([:=]
), indicating that they are escaped characters.
REGEX_3
:var REGEX_3 = /"{0,1}cpf"{0,1}[=:]"{0,1}([^ ,]*)/;
This regex pattern is similar to REGEX_1
, but with a different capture group for the character class [^ ,]*
. This captures any single character that is not whitespace ([^
represents "not", and
represents whitespace).
Library: None
There are no libraries used in this benchmark.
Special JS Feature/Syntax: None
There are no special JavaScript features or syntaxes used in this benchmark.
Pros and Cons of Each Approach
REGEX_1
:cpf=12
without quotes)REGEX_2
:[:=]
)REGEX_1
REGEX_3
:[:=]
)[^ ,]*
)REGEX_1
or REGEX_2
Other Alternatives
If you need to test regex patterns in JavaScript, other alternatives include:
Keep in mind that the choice of alternative depends on your specific use case and requirements.