var reConstructor = new RegExp('^[0-9a-fA-F]{24}$')
var reLiteral = /^[0-9a-fA-F]{24}$/
(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 |
---|---|
new RegExp() | |
Literal |
Test name | Executions per second |
---|---|
new RegExp() | 9701429.0 Ops/sec |
Literal | 405860384.0 Ops/sec |
Let's break down the provided benchmark and explain what is tested, compared, and considered.
Benchmark Overview
The MeasureThat.net website provides a microbenchmarking platform to compare performance of different approaches in JavaScript. The current benchmark measures the performance of two ways to create regular expressions:
new
keyword.new
keyword.Benchmark Definition
The benchmark definition is provided as JSON, which includes:
reConstructor
and reLiteral
.Individual Test Cases
The benchmark consists of two individual test cases:
(new RegExp('^[0-9a-fA-F]{24}$')).test('132abc67219f019afe12901a')
new RegExp()
/^[0-9a-fA-F]{24}$/ .test('132abc67219f019afe12901a')
Literal
Library and Purpose
In the script preparation code, two RegExp objects are created:
reConstructor = new RegExp('^[0-9a-fA-F]{24}$')
: This creates a new RegExp object using the constructor.reLiteral = /^[0-9a-fA-F]{24}$/
: This is a literal regex pattern without the new
keyword.The test()
method is called on both RegExp objects with the same input string to measure their performance.
Special JS Feature/Syntax
There are no special JavaScript features or syntax used in this benchmark. It only relies on standard JavaScript functionality.
Pros and Cons of Different Approaches
new RegExp('pattern', 'flags')
).Other Alternatives
If you wanted to create similar benchmarks for other ways to create RegExp objects or test different aspects of performance, consider the following:
/pattern.flags/
).new RegExp(pattern, flags)
).Keep in mind that the specific benchmarks and test cases will depend on your use case and requirements.