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]+)/;
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")
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
REGEX_1 | |
REGEX_2 |
Test name | Executions per second |
---|---|
REGEX_1 | 10770930.0 Ops/sec |
REGEX_2 | 5535222.5 Ops/sec |
This benchmark tests the performance of two regular expressions (REGEX_1
and REGEX_2
) when used with the .test()
method to find a specific pattern in a string.
Options Compared:
/\"{0,1}cpf\"{0,1}[:=]\"{0,1}([\\w\\s]{1,})/
/\"{0,1}cpf\"{0,1}[:=]\\\"{0,1}([\\w\\s]+)/
Both regular expressions aim to find the text "cpf" followed by an equal sign or a colon and then one or more word characters or spaces. They essentially look for the pattern "cpf=[...]" or "cpf:[...]".
Pros/Cons:
Key Considerations:
\
, backslash, is crucial for escaping special characters in regular expressions. Without proper escaping, these characters would have their literal meaning interpreted instead of their intended regex functionality (e.g., \
matches a backslash character literally).Other Alternatives:
While regular expressions are powerful, there might be alternative approaches for this specific task depending on the context:
indexOf()
or substring()
to extract the desired information if the pattern is relatively simple.Important Note: The provided benchmark results only show the performance on a specific machine and browser configuration. Actual performance can vary significantly depending on the system hardware, software environment, and other factors.