Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
RegExp constructor vs inline literal
(version: 0)
Testing performance when using a new RegExp object vs a literal regex
Comparing performance of:
new RegExp() vs Inline literal
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var reConstructor = new RegExp('^[0-9a-fA-F]{24}$')
Tests:
new RegExp()
reConstructor.test('132abc67219f019afe12901a')
Inline 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()
Inline literal
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
new RegExp()
4832716.5 Ops/sec
Inline literal
4536432.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! **Benchmark Overview** The provided benchmark measures the performance difference between using the `RegExp` constructor to create a new regular expression object versus using an inline literal (a string enclosed in forward slashes `/`). The test consists of two individual test cases: one that uses the `new RegExp()` method and another that uses an inline literal. **What is tested?** In this benchmark, we're testing the performance difference between creating a new `RegExp` object using the constructor versus using an inline literal. Specifically: * `new RegExp('^[0-9a-fA-F]{24}$')`: Creates a new regular expression object with the specified pattern. * `/^[0-9a-fA-F]{24}$/`: Uses an inline literal to define the same regular expression. **Options compared** We're comparing two approaches: 1. **`new RegExp()`**: Creates a new `RegExp` object, which involves parsing and compiling the regular expression pattern. 2. **Inline Literal (`/...$/`)**: Uses an inline literal to define the regular expression pattern, which is essentially a string that represents the regex. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * `new RegExp()`: + Pros: Can be more readable and easier to maintain for complex regex patterns. + Cons: May incur additional overhead due to parsing and compilation, which can impact performance. * Inline Literal (`/...$/`): + Pros: Typically faster and more efficient than using the `RegExp` constructor. + Cons: Can be less readable and harder to maintain for complex regex patterns. **Library usage** In this benchmark, no specific JavaScript library is used. The test cases only involve built-in JavaScript functionality. **Special JS feature or syntax** There are a few aspects of JavaScript that might be worth noting: * **Template literals**: Although not explicitly mentioned in the benchmark, template literals (introduced in ECMAScript 2015) provide a concise way to define inline regex patterns. However, this feature is not used in the provided benchmark. * **String methods**: The test cases utilize string methods like `test()` and `includes()`, which are part of the JavaScript standard library. **Other alternatives** If you wanted to explore alternative approaches, consider the following: * Using a dedicated regex engine or library, such as `regex-test` (a Node.js module) or `regex-quickcheck` (a command-line tool). * Employing other optimization techniques, like caching or memoization, to improve performance. Keep in mind that these alternatives might not be directly applicable to the specific use case presented in this benchmark.
Related benchmarks:
RegExp constructor vs literal vs premade, constructing in function, inline the test
RegExp constructor vs literal vs RegExp+Literal
RegExp constructor vs literal vs inline literal
RegExp constructor vs literal (re-do creation)
Comments
Confirm delete:
Do you really want to delete benchmark?