Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegExp constructor vs literal + instanciation
(version: 1)
Testing performance when using a new RegExp object vs a literal regex
Comparing performance of:
new RegExp() vs Literal
Created:
one year ago
by:
Guest
Go to the latest result
Tests:
new RegExp()
new RegExp('^[0-9a-fA-F]{24}$').test('132abc67219f019afe12901a')
Literal
/^[0-9a-fA-F]{24}$/.test('132abc67219f019afe12901a')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
new RegExp()
Literal
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Literal
14.9M/s
new RegExp()
8.6M/s
View exact numbers
Test name
Executions per second
✓
Literal
14,857,931 Ops/sec
new RegExp()
8,591,375 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark titled **"RegExp constructor vs literal + instantiation"** is designed to evaluate the performance differences between using a **RegExp constructor** (`new RegExp`) and a **regular expression literal** (defined with slashes `/.../`) when testing patterns against input strings in JavaScript. The test specifically checks the execution speed of both approaches for running regular expression tests. ### Test Cases 1. **Test Case: `new RegExp('^[0-9a-fA-F]{24}$').test('132abc67219f019afe12901a')`** - **Test Name**: "new RegExp()" - This test creates a new instance of a regular expression object using the constructor `new RegExp()`. It evaluates whether the input string `'132abc67219f019afe12901a'` matches the pattern defined—in this case, checking for a hexadecimal string of exactly 24 characters. 2. **Test Case: `/^[0-9a-fA-F]{24}$/.test('132abc67219f019afe12901a')`** - **Test Name**: "Literal" - This test applies a regular expression literal to check the same input against the same criteria as the previous test using a more concise syntax with a literal. ### Performance Results The benchmark results indicate: - **Literal Method**: Approximately **14,857,931 executions per second**. - **RegExp Constructor Method**: Approximately **8,591,375 executions per second**. ### Pros and Cons of Each Approach **1. Regular Expression Literal** - **Pros**: - Generally faster execution, as indicated by the benchmark results. - Easier to read and write for most use cases, as it follows a more concise syntax. - Compiled at parse time, which can lead to improved speed since the JavaScript engine can optimize it directly. - **Cons**: - Less flexible if you need to dynamically construct the pattern. You cannot use variables to form the regex easily. **2. RegExp Constructor** - **Pros**: - Allows for dynamic creation of regex patterns using variables, making it easy to alter the expression at runtime. - Useful in scenarios where the pattern requires runtime information, such as user input. - **Cons**: - Slower performance compared to literals. - Requires the pattern to be provided as a string, which can be less readable and might need additional escape characters. ### Other Considerations - In typical scenarios where the pattern is static, favoring regex literals can improve performance. - In cases requiring dynamic regex patterns, the constructor becomes a necessary choice despite the performance overhead. - If you need to use patterns periodically, pre-compiling a regex with the constructor and reusing it can mitigate some performance drawbacks. ### Alternatives - **String Matching**: If exact string matches are needed without regex complexity, using simple string comparison methods like `String.includes()`, `String.startsWith()`, or `String.endsWith()` might yield better performance. - **Third-Party Libraries**: Libraries like **XRegExp** provide extended capabilities for regex that go beyond standard JavaScript regex features and may optimize performance in some specific complex use cases. By understanding the differences between using a regex constructor and regex literals, engineers can make informed decisions tailored to their specific application requirements, balancing readability, performance, and flexibility.
Related benchmarks
RegExp constructor vs literal
RegExp constructor vs literal vs RegExp+Literal
RegExp constructor vs literal vs inline literal
RegExp constructor vs inline literal
RegExp constructor vs literal (re-do creation)
Test RegExp Performance
Comments
Confirm delete:
Do you really want to delete benchmark?