Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegExp constructor vs literal vs premade, constructing in function, inline the test
(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
Created:
6 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'); } var premadeLiteral = /^[0-9a-fA-F]{24}$/; function getRePremadeLiteral() { return premadeLiteral.test('132abc67219f019afe12901a'); }
Tests:
new RegExp()
getReConstructor()
RegExp() factory
getReFactory()
Literal
getReLiteral()
Literal premade
getRePremadeLiteral()
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
new RegExp()
RegExp() factory
Literal
Literal premade
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36
Browser/OS:
Chrome 123 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
new RegExp()
5547883.5 Ops/sec
RegExp() factory
5522947.0 Ops/sec
Literal
13249286.0 Ops/sec
Literal premade
9706220.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into explaining the benchmark and its various aspects. **Benchmark Description:** The benchmark measures the performance of different approaches to create a regular expression (regex) in JavaScript: 1. `new RegExp()` constructor 2. Literal regex (i.e., creating a regex pattern as a string) 3. Premade literal regex (i.e., using an existing compiled regex object) **Options Compared:** * **`new RegExp()` constructor**: Creates a new regex object from scratch. * **Literal regex**: Creates a regex pattern as a string, without compiling it beforehand. * **Premade literal regex**: Uses an existing compiled regex object to create the regex. **Pros and Cons of each approach:** * **`new RegExp()` constructor**: + Pros: Can create complex regex patterns with ease. Compiles the regex on first use, which can improve performance. + Cons: Creates a new regex object every time it's used, which can lead to slower performance compared to using an existing compiled regex. * **Literal regex**: + Pros: Fast and efficient, as the regex is created immediately. However, creating complex patterns manually can be cumbersome. + Cons: Requires manual pattern creation, which can increase development time. Also, compiling the regex on first use may lead to slower performance. * **Premade literal regex**: + Pros: Can reuse an existing compiled regex object, reducing compilation overhead and improving performance. + Cons: Requires creating a pre-compiled regex object beforehand, which can increase development time. **Library Used:** None There are no external libraries used in this benchmark. **Special JS Features/Syntax:** None mentioned No special JavaScript features or syntax are used in this benchmark. **Other Alternatives:** * **Regex literals with `RegExp()`**: Instead of using the constructor, you can use a regex literal with `RegExp()`. For example: `var myRegex = RegExp('^[0-9a-fA-F]{24}$');` * **Using a regex engine**: Some JavaScript engines, like SpiderMonkey, have built-in regex engines that provide optimized performance. **Benchmark Preparation Code Explanation:** The preparation code defines four functions: * `getReConstructor()`: Creates a new regex object using the constructor. * `getReFactory()`: Creates a regex object using the factory method (`RegExp()`). * `getReLiteral()`: Creates a regex pattern as a string and uses it to test a string. * `getRePremadeLiteral()`: Uses an existing compiled regex object to test a string. The code also defines two pre-compiled regex objects: `premadeLiteral` and its usage in `getRePremadeLiteral()`. **Benchmark Result Explanation:** The benchmark results show the execution count per second for each approach on a specific machine. The highest performance is achieved by using the literal regex, followed closely by the premade literal regex. The constructor-based approach has lower performance due to the overhead of creating a new regex object every time it's used. Keep in mind that these results may vary depending on your specific environment and JavaScript engine.
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 2
RegExp constructor vs literals, with variations
Comments
Confirm delete:
Do you really want to delete benchmark?