new RegExp('^[0-9a-fA-F]{24}$').test('132abc67219f019afe12901a')
/^[0-9a-fA-F]{24}$/.test('132abc67219f019afe12901a')
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
using new RegExp() | |
using literal |
Test name | Executions per second |
---|---|
using new RegExp() | 2228456.8 Ops/sec |
using literal | 8483451.0 Ops/sec |
Let's dive into the benchmark.
What is being tested?
The provided benchmark compares two approaches to test a regular expression (regexp) pattern: literal creation and regexp creation using the new RegExp()
constructor. The test cases verify whether the regexp engine can correctly identify a hexadecimal string as valid or not, given an input string that matches this pattern.
Options compared:
/^[0-9a-fA-F]{24}$/.test('132abc67219f019afe12901a')
new RegExp()
: new RegExp('^[0-9a-fA-F]{24}$').test('132abc67219f019afe12901a')
Pros and Cons of each approach:
new RegExp()
, since it requires parsing the string literal at runtime.new RegExp()
:Library used:
None.
Special JS feature or syntax:
The benchmark uses JavaScript's built-in RegExp
object and its methods (test()
), which are part of the ECMAScript standard. No special features or syntax are used beyond what's standard in JavaScript.
Other alternatives:
For comparison, you could also use alternative approaches like:
regexpkit
, which provides a more efficient way to create and optimize regular expressions.However, these alternatives are not part of the original benchmark, so they wouldn't affect the comparison between literal creation and regexp creation using new RegExp()
.