Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegExp constructor vs literal (re-do creation)
(version: 0)
Testing performance when using a new RegExp object vs a literal regex
Comparing performance of:
new RegExp() vs Literal
Created:
2 years ago
by:
Guest
Jump 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()
(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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
new RegExp()
1396474.4 Ops/sec
Literal
3394795.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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: 1. **RegExp constructor**: Creating a new RegExp object using the `new` keyword. 2. **Literal regex**: Using a literal regex pattern without the `new` keyword. **Benchmark Definition** The benchmark definition is provided as JSON, which includes: * **Name**: A unique name for the benchmark. * **Description**: A brief description of the test case. * **Script Preparation Code**: The JavaScript code that prepares the script before execution. In this case, it creates two RegExp objects: `reConstructor` and `reLiteral`. * **Html Preparation Code**: Not applicable in this case. **Individual Test Cases** The benchmark consists of two individual test cases: 1. **new RegExp()** * Benchmark Definition: `(new RegExp('^[0-9a-fA-F]{24}$')).test('132abc67219f019afe12901a')` * Test Name: `new RegExp()` 2. **Literal** * Benchmark Definition: `/^[0-9a-fA-F]{24}$/ .test('132abc67219f019afe12901a')` * Test Name: `Literal` **Library and Purpose** In the script preparation code, two RegExp objects are created: 1. `reConstructor = new RegExp('^[0-9a-fA-F]{24}$')`: This creates a new RegExp object using the constructor. 2. `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** 1. **RegExp constructor**: * Pros: Can be useful when creating complex regex patterns with flags (e.g., `new RegExp('pattern', 'flags')`). * Cons: May incur overhead due to the creation of a new object. 2. **Literal regex**: * Pros: Faster execution, as it avoids object creation overhead. * Cons: Limited to simple regex patterns without 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: 1. Using a RegExp literal with flags (e.g., `/pattern.flags/`). 2. Creating a RegExp object using a string constructor (e.g., `new RegExp(pattern, flags)`). 3. Comparing performance of different regex engines (e.g., built-in vs. third-party libraries like RegEx.js). Keep in mind that the specific benchmarks and test cases will depend on your use case and requirements.
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
Comments
Confirm delete:
Do you really want to delete benchmark?