var string = "Hello world!";
var regex = /[A-Z][a-z]+ [a-z]+/;
/[A-Z][a-z]+ [a-z]+/.test(string);
regex.test(string);
string.match(/[A-Z][a-z]+ [a-z]+/);
string.match(regex);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
inline regex test (1) | |
reference regex test (2) | |
inline match (3) | |
reference match (4) |
Test name | Executions per second |
---|---|
inline regex test (1) | 19269174.0 Ops/sec |
reference regex test (2) | 19999594.0 Ops/sec |
inline match (3) | 12673832.0 Ops/sec |
reference match (4) | 13071716.0 Ops/sec |
What is being tested?
MeasureThat.net is testing the performance of different approaches to regular expression matching in JavaScript:
test()
method on a RegExp object (regex.test(string);
).test()
method on a String prototype (string.match(regex);
).match()
method on a String (string.match(/[A-Z][a-z]+ [a-z]+/);
).Options compared
The benchmark is comparing two approaches:
test()
method on a RegExp object.match()
method on a String.Pros and cons of each approach
Reference approach (using test()
method on a RegExp object)
Pros:
Cons:
Inline approach (creating an inline RegExp)
Pros:
Cons:
Other considerations
Library usage (none)
There is no library used in this benchmark. The RegExp object and String prototype are native JavaScript objects.
Special JS feature or syntax (none)
This benchmark does not use any special JavaScript features or syntax beyond standard regular expression matching.
Now, let's discuss the latest benchmark results:
The latest results show that the inline approach leads to faster execution times for all test cases. However, it's essential to note that these results may vary depending on specific requirements and project constraints.
Alternatives
Other alternatives to consider when choosing between these approaches include:
String.prototype.replace()
method with a callback function.