Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegExp constructor vs literals, with variations
(version: 0)
Testing performance when using a new RegExp object vs a literal regex
Comparing performance of:
new RegExp() vs RegExp() factory vs Literal vs Literal premade vs Premade constructor vs Premade constructor 2
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function getReConstructor() { return new RegExp('^[0-9a-fA-F]{24}$').test('132abc67219f019afe12901a'); } function getReFactory() { return RegExp('^[0-9a-fA-F]{24}$').test('132abc67219f019afe12901a'); } function getReLiteral() { return /^[0-9a-fA-F]{24}$/.test('132abc67219f019afe12901a'); } const premadeLiteral = /^[0-9a-fA-F]{24}$/; function getRePremadeLiteral() { return premadeLiteral.test('132abc67219f019afe12901a'); } const premadeConstructor = new RegExp('^[0-9a-fA-F]{24}$'); function getRePremadeConstructor() { return premadeConstructor.test('132abc67219f019afe12901a'); } const premadeConstructor2 = new RegExp(/^[0-9a-fA-F]{24}$/); function getRePremadeConstructor2() { return premadeConstructor2.test('132abc67219f019afe12901a'); }
Tests:
new RegExp()
getReConstructor()
RegExp() factory
getReFactory()
Literal
getReLiteral()
Literal premade
getRePremadeLiteral()
Premade constructor
getRePremadeConstructor()
Premade constructor 2
getRePremadeConstructor2()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
new RegExp()
RegExp() factory
Literal
Literal premade
Premade constructor
Premade constructor 2
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the benchmark and explain what's being tested, compared options, pros/cons of each approach, and other considerations. **Benchmark Overview** The benchmark measures the performance of creating regular expressions (regex) in JavaScript using different methods: literal strings, RegExp constructors, and pre-made literals. The goal is to compare the execution speed of these approaches. **Test Cases** There are six test cases: 1. `new RegExp()`: Creating a new RegExp object using the constructor. 2. `RegExp() factory`: Creating a new RegExp object using the factory function (not shown in the benchmark definition, but assumed to be similar to `getReFactory()`). 3. Literal: Using a literal string as a regex pattern. 4. Premade Literal: Using a pre-made literal string as a regex pattern. 5. Premade Constructor: Creating a new RegExp object using a pre-made RegExp constructor. 6. Premade Constructor 2: Similar to the previous test case, but with a slightly different RegExp constructor. **Comparison of Approaches** The main differences between these approaches are: * **Literal strings**: These patterns are created as strings and then used as regex patterns. This approach is straightforward but might be slower due to string creation overhead. * **RegExp constructors**: Creating new RegExp objects using the constructor is a common method in JavaScript. The benchmark tests how fast this approach is compared to literals. * **Pre-made literals**: Pre-made literals are created beforehand and reused throughout the benchmark. This approach can reduce overhead by avoiding repeated string creation. **Pros and Cons of Each Approach** 1. **Literal strings**: * Pros: Easy to understand, minimal overhead. * Cons: Can be slower due to string creation, less efficient for complex patterns. 2. **RegExp constructors**: * Pros: Fast, flexible, suitable for complex patterns. * Cons: Requires careful configuration and might be slower than pre-made literals. 3. **Pre-made literals**: * Pros: Reduces overhead by avoiding repeated string creation, easy to maintain. * Cons: Requires more setup, might not be as efficient for simple patterns. **Other Considerations** The benchmark also takes into account the device platform (Desktop), browser version (Chrome 103), and operating system (Mac OS X 10.15.7). These factors can affect performance due to differences in JavaScript engine optimizations, hardware capabilities, or browser-specific features. **Library Usage** None of the test cases explicitly use a JavaScript library. However, some libraries might be used implicitly through the use of built-in functions like `RegExp()` or `test()`, which are part of the JavaScript standard library. **Special JS Features/Syntax** There is no explicit mention of special JS features or syntax being used in this benchmark. The focus is on comparing different approaches to creating regular expressions.
Related benchmarks:
RegExp constructor vs literal, constructing in function
RegExp constructor vs literal vs premade, constructing in function
RegExp constructor vs literal vs premade, constructing in function, inline the test
RegExp constructor vs literal vs premade, constructing in function, inline the test 2
Comments
Confirm delete:
Do you really want to delete benchmark?