Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegExp constructor vs literal vs RegExp+Literal
(version: 0)
Testing performance when using a new RegExp object vs a literal regex
Comparing performance of:
new RegExp() vs Literal vs new RegExp(Literal)
Created:
6 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}$/ var reConstLiteral = new RegExp(/^[0-9a-fA-F]{24}$/)
Tests:
new RegExp()
reConstructor.test('132abc67219f019afe12901a')
Literal
reLiteral.test('132abc67219f019afe12901a')
new RegExp(Literal)
var reConstLiteral = new RegExp(/^[0-9a-fA-F]{24}$/)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
new RegExp()
Literal
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/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
new RegExp()
16403342.0 Ops/sec
Literal
18518956.0 Ops/sec
new RegExp(Literal)
13391357.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided benchmark is designed to measure the performance of three different approaches for testing regular expressions in JavaScript: 1. Creating a new `RegExp` object using the constructor (`new RegExp()`). 2. Using a literal regex (a string that represents a valid regex pattern, without creating a new `RegExp` object). 3. Combining the creation of a new `RegExp` object with a literal regex. **Library and Syntax** In this benchmark, no custom JavaScript libraries are used beyond the built-in `RegExp` class. However, it's worth noting that using `RegExp` literals can be more efficient than creating a new `RegExp` object for simple patterns, as it avoids the overhead of compilation. **Special JS Feature** There is no special JavaScript feature or syntax being tested in this benchmark, other than the usage of regular expressions (Regex) themselves. The focus is on comparing the performance of different approaches to create and use Regex objects. **Options Compared** The benchmark compares the performance of three options: 1. **`new RegExp()`**: Creates a new `RegExp` object using the constructor. 2. **Literal regex**: Uses a string that represents a valid regex pattern, without creating a new `RegExp` object. 3. **`new RegExp(Literal)`**: Combines the creation of a new `RegExp` object with a literal regex. **Pros and Cons** Here are some pros and cons for each approach: * **`new RegExp()`**: + Pros: Can be used to create complex, dynamic Regex patterns. + Cons: Creates an overhead due to compilation, which can impact performance. * **Literal regex**: + Pros: Avoids the overhead of compilation, making it faster for simple patterns. + Cons: Limited flexibility and cannot be used to create complex Regex patterns. * **`new RegExp(Literal)`**: + Pros: Combines the benefits of both approaches, allowing for more flexibility while still avoiding compilation overhead. + Cons: Still incurs some overhead due to creating a new `RegExp` object. **Other Considerations** When choosing between these approaches, consider the trade-off between performance and flexibility. For simple, well-defined Regex patterns, literal regex may be the best choice. However, for more complex or dynamic patterns, creating a new `RegExp` object using the constructor or combining both methods (`new RegExp(Literal)`) may be more suitable. **Alternatives** If you're looking for alternatives to this benchmark, consider testing other aspects of JavaScript performance, such as: * Function call performance * Object creation and iteration performance * String manipulation performance (e.g., concatenation, substring extraction) * Array manipulation performance (e.g., push, pop, indexing) You can also explore benchmarks that focus on specific areas, like: * WebAssembly (WASM) performance in JavaScript * V8 engine optimizations and performance characteristics * Performance comparison between different JavaScript engines or interpreters
Related benchmarks:
RegExp constructor vs 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?