var reConstructor = new RegExp('^[0-9a-fA-F]{24}$')
reConstructor.test('132abc67219f019afe12901a')
/^[0-9a-fA-F]{24}$/.test('132abc67219f019afe12901a')
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
new RegExp() | |
Inline literal |
Test name | Executions per second |
---|---|
new RegExp() | 17209456.0 Ops/sec |
Inline literal | 16878812.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks!
Benchmark Overview
The provided benchmark measures the performance difference between using the RegExp
constructor to create a new regular expression object versus using an inline literal (a string enclosed in forward slashes /
). The test consists of two individual test cases: one that uses the new RegExp()
method and another that uses an inline literal.
What is tested?
In this benchmark, we're testing the performance difference between creating a new RegExp
object using the constructor versus using an inline literal. Specifically:
new RegExp('^[0-9a-fA-F]{24}$')
: Creates a new regular expression object with the specified pattern./^[0-9a-fA-F]{24}$/
: Uses an inline literal to define the same regular expression.Options compared
We're comparing two approaches:
new RegExp()
: Creates a new RegExp
object, which involves parsing and compiling the regular expression pattern./...$/
): Uses an inline literal to define the regular expression pattern, which is essentially a string that represents the regex.Pros and Cons
Here's a brief summary of the pros and cons of each approach:
new RegExp()
:/...$/
):RegExp
constructor.Library usage
In this benchmark, no specific JavaScript library is used. The test cases only involve built-in JavaScript functionality.
Special JS feature or syntax
There are a few aspects of JavaScript that might be worth noting:
test()
and includes()
, which are part of the JavaScript standard library.Other alternatives
If you wanted to explore alternative approaches, consider the following:
regex-test
(a Node.js module) or regex-quickcheck
(a command-line tool).Keep in mind that these alternatives might not be directly applicable to the specific use case presented in this benchmark.