function getReConstructor() {
return new RegExp('^[0-9a-fA-F]{24}$').test('132abc67219f019afe12901a');
}
function getReFactory() {
return RegExp('^[0-9a-fA-F]{24}$').test('132abc67219f019afe12901a');
}
function getReLiteral() {
return /^[0-9a-fA-F]{24}$/.test('132abc67219f019afe12901a');
}
const premadeLiteral = /^[0-9a-fA-F]{24}$/;
function getRePremadeLiteral() {
return premadeLiteral.test('132abc67219f019afe12901a');
}
const premadeConstructor = new RegExp('^[0-9a-fA-F]{24}$');
function getRePremadeConstructor() {
return premadeConstructor.test('132abc67219f019afe12901a');
}
const premadeConstructor2 = new RegExp(/^[0-9a-fA-F]{24}$/);
function getRePremadeConstructor2() {
return premadeConstructor2.test('132abc67219f019afe12901a');
}
getReConstructor()
getReFactory()
getReLiteral()
getRePremadeLiteral()
getRePremadeConstructor()
getRePremadeConstructor2()
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
new RegExp() | |
RegExp() factory | |
Literal | |
Literal premade | |
Premade constructor | |
Premade constructor 2 |
Test name | Executions per second |
---|---|
new RegExp() | 2716764.2 Ops/sec |
RegExp() factory | 2770801.2 Ops/sec |
Literal | 7673892.0 Ops/sec |
Literal premade | 7832211.5 Ops/sec |
Premade constructor | 7840322.5 Ops/sec |
Premade constructor 2 | 7869570.5 Ops/sec |
I'll break down the benchmark and explain what's being tested, compared options, pros/cons of each approach, and other considerations.
Benchmark Overview
The benchmark measures the performance of creating regular expressions (regex) in JavaScript using different methods: literal strings, RegExp constructors, and pre-made literals. The goal is to compare the execution speed of these approaches.
Test Cases
There are six test cases:
new RegExp()
: Creating a new RegExp object using the constructor.RegExp() factory
: Creating a new RegExp object using the factory function (not shown in the benchmark definition, but assumed to be similar to getReFactory()
).Comparison of Approaches
The main differences between these approaches are:
Pros and Cons of Each Approach
Other Considerations
The benchmark also takes into account the device platform (Desktop), browser version (Chrome 103), and operating system (Mac OS X 10.15.7). These factors can affect performance due to differences in JavaScript engine optimizations, hardware capabilities, or browser-specific features.
Library Usage
None of the test cases explicitly use a JavaScript library. However, some libraries might be used implicitly through the use of built-in functions like RegExp()
or test()
, which are part of the JavaScript standard library.
Special JS Features/Syntax
There is no explicit mention of special JS features or syntax being used in this benchmark. The focus is on comparing different approaches to creating regular expressions.