Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
RegExp constructor vs literal using exec
(version: 0)
Benchmark.js
Testing performance when using a new RegExp object vs a literal regex with exec method
Comparing performance of:
new RegExp() vs Literal
Created:
3 years ago
by:
Guest
Go to the latest result
Script Preparation code:
var reConstructor = new RegExp('^[0-9a-fA-F]{24}$') var reLiteral = /^[0-9a-fA-F]{24}$/
Tests:
new RegExp()
reConstructor.exec('132abc67219f019afe12901a')
Literal
reLiteral.exec('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:
11 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36 Edg/141.0.0.0
Browser/OS:
Chrome 141 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
new RegExp()
16.8M/s
Literal
16.7M/s
View exact numbers
Test name
Executions per second
✓
new RegExp()
16,826,312 Ops/sec
Literal
16,741,214 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares the performance of two approaches: 1. **RegExp constructor**: Creating a new RegExp object using the `new` keyword. 2. **Literal regex with exec method**: Using a literal regex string and calling the `exec()` method on it. **Options Compared** The benchmark is comparing the execution speed of these two approaches when searching for a specific pattern in a given string. **Pros and Cons of Each Approach** 1. **RegExp constructor**: * Pros: Can be more flexible, as you can create complex regex patterns using the `pattern` and `flags` properties. * Cons: May lead to slower performance due to the overhead of creating a new RegExp object. 2. **Literal regex with exec method**: * Pros: Can be faster for simple cases, as it avoids the overhead of creating a new RegExp object. * Cons: Limited flexibility, as you're stuck with a fixed pattern. **Other Considerations** * The benchmark uses the `exec()` method, which can stop at the first match, potentially affecting performance. * The strings used in the test are 24 characters long, which is likely the maximum length of an Ethereum address (used for testing). **Library Used** None explicitly mentioned. However, RegExp objects use the ECMAScript Standard library. **Special JS Feature or Syntax** No special features or syntax are being tested in this benchmark. **Alternative Approaches** For similar benchmarks: * Using a regex engine like libregex or regex-js * Comparing other string searching methods, such as `indexOf()` or `includes()` * Testing performance with different regex flags (e.g., `g`, `i`, `m`) Keep in mind that benchmarking can be highly dependent on the specific test case and JavaScript environment. The results may vary depending on the execution context.
Related benchmarks
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)
Comments
Confirm delete:
Do you really want to delete benchmark?