var reStr = '^[0-9]+$';
var re = new RegExp(re);
!re.test('123');
!'123'.match(reStr);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
RegExp.test() | |
String.match() |
Test name | Executions per second |
---|---|
RegExp.test() | 30955626.0 Ops/sec |
String.match() | 5042227.5 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
What is being tested?
The provided JSON represents a benchmark that compares two approaches for testing regular expressions (REs) in JavaScript: RegExp.test()
and String.match()
. The benchmark measures the performance difference between these two methods when used with a specific regular expression pattern (^[0-9]+$
) on a string literal '123'
.
Options compared
Two options are being compared:
RegExp.test()
: This method tests if the entire string matches the regular expression.String.match()
: This method returns an array of matches or null
if no match is found.Pros and Cons of each approach:
RegExp.test()
:String.match()
for complex patterns or when used with multiple matches.String.match()
:Library and purpose
In this benchmark, RegExp
is a built-in JavaScript library that provides regular expression functionality. Its purpose is to test if a given string matches a specified pattern.
Special JS feature or syntax
None mentioned in the provided code snippet.
Other alternatives
If you're interested in exploring alternative approaches, consider:
String.prototype
methods or other libraries like regex-execution
. This approach would require significant effort and optimization to match the performance of built-in engines.Benchmark preparation code
The provided script preparation code creates a regular expression object (re
) using the pattern ^[0-9]+$
, which matches only digits from start to end. The test cases use this regular expression with different approaches: RegExp.test()
and String.match()
.
Individual test cases
Each test case has two parts:
!re.test('123')
or '123'.match(reStr)
).Latest benchmark result
The provided JSON contains two benchmark results for each test case:
These results provide insight into how different browsers and environments perform when running these specific test cases.