Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegExp constructor vs literal 2
(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
Jump to the latest result
Script Preparation code:
const str = '132abc67219f019afe12901a'
Tests:
new RegExp()
new RegExp('^[0-9a-fA-F]{24}$').test(str)
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; rv:137.0) Gecko/20100101 Firefox/137.0
Browser/OS:
Firefox 137 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
new RegExp()
8519526.0 Ops/sec
Literal
15364923.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark titled "RegExp constructor vs literal 2" tests the performance of two different approaches to create and use regular expressions in JavaScript: using the `RegExp` constructor and using a regular expression literal. ### Options Compared 1. **`new RegExp()` Constructor**: - **Test Case**: `new RegExp('^[0-9a-fA-F]{24}$').test(str)` - Here, a new instance of a regular expression is created using the `RegExp` constructor. This method is useful when the regular expression needs to be dynamically generated or when it needs to include variables. 2. **Regular Expression Literal**: - **Test Case**: `/^[0-9a-fA-F]{24}$/.test('132abc67219f019afe12901a')` - This approach uses a regex literal directly in the code. It's more concise and is typically preferred when the regex pattern is static and known ahead of time. ### Performance Results Based on the benchmark results, the performance of each approach varied significantly: - **Literal**: - **Executions Per Second**: 15,364,923.0 - This method performed substantially better than the constructor method. The regex literal appears to have less overhead, making it faster for execution. - **`new RegExp()`**: - **Executions Per Second**: 8,519,526.0 - Utilizing the `RegExp` constructor resulted in a lower performance compared to the literal method, likely due to the additional overhead of creating a new object each time. ### Pros and Cons #### `new RegExp()` Constructor - **Pros**: - Flexibility: Allows for dynamic regex creation if patterns are driven by user input or other variables. - Useful in scenarios where the regex pattern is not known until runtime. - **Cons**: - Performance: Slower than using literals due to object creation overhead. - More verbose syntax, which may reduce code readability. #### Regular Expression Literal - **Pros**: - Performance: Generally faster due to reduced overhead, as the regex engine optimizes regex literals better. - Readability: More concise and easier to understand for static patterns. - **Cons**: - Inflexibility: Cannot be easily modified at runtime without additional code to construct a string and then convert it into a regex object. ### Other Considerations and Alternatives 1. **Choosing Between the Two**: Decision-making should consider both performance needs and the flexibility required by the application. If regex patterns are known at development time and will remain static, literals are the better choice. If they need to change based on user input or other data sources, the `RegExp` constructor is necessary. 2. **Alternative Approaches**: Other alternatives could involve optimizing regex patterns or using libraries like **XRegExp**, which provide extended features over the native regex functionality in JavaScript. Such libraries often promote better readability and offer more powerful functions for advanced regex handling, although they may add their own performance overhead. 3. **General Use Cases**: Regular expressions are commonly used for validations, pattern matching, and search-and-replace operations in strings, making their performance impacts potentially significant depending on the context in which they are applied. In summary, when working with regular expressions in JavaScript, choosing between a literal and a constructor should be guided by the need for performance versus flexibility. The benchmark clearly indicates that for fixed patterns, using a regular expression literal is the most efficient choice.
Related benchmarks:
RegExp constructor vs literal
RegExp constructor vs literal, constructing in function
RegExp constructor vs literal vs RegExp+Literal
Compare regexp vs literal creation and test
RegExp constructor vs literals, with variations
RegExp constructor vs literal vs inline literal
RegExp constructor vs inline literal
RegExp constructor vs literal (re-do creation)
RegExp constructor vs literal + instanciation
Comments
Confirm delete:
Do you really want to delete benchmark?