Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
how fast is regex?
(version: 1)
Comparing performance of:
instantiate new regex vs use existing regex
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
const prepared = /[.*+?^${}()|[\]\\]/g;
Tests:
instantiate new regex
const blah = "whatever" const r = blah.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")
use existing regex
const blah = "whatever" const r = blah.replace(prepared, "\\$&")
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
instantiate new regex
use existing regex
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 days ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:150.0) Gecko/20100101 Firefox/150.0
Browser/OS:
Firefox 150 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
instantiate new regex
35607608.0 Ops/sec
use existing regex
34647956.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark titled "how fast is regex?" compares the performance of two different approaches to using regular expressions in JavaScript: instantiating a new regular expression object each time versus using a pre-prepared regex object. ### Options Compared 1. **Instantiate New Regex**: - **Test Code**: ```javascript const blah = "whatever"; const r = blah.replace(/[.*+?^${}()|[\\]\\\\]/g, "\\$&"); ``` - **Description**: In this test, a new regular expression is created directly in the `replace()` function. Every time this line of code executes, a new regex object is instantiated. 2. **Use Existing Regex**: - **Test Code**: ```javascript const blah = "whatever"; const r = blah.replace(prepared, "\\$&"); ``` - **Description**: This test uses a regex that has been prepared beforehand (stored in the `prepared` variable). The regex is compiled only once and can be reused multiple times in the code. ### Pros and Cons #### 1. Pros and Cons of Instantiating New Regex: - **Pros**: - **Simplicity**: Writing regex inline can make the code easier to understand at first glance without needing separate variable declarations. - **Cons**: - **Performance Overhead**: Each execution creates a new regex object. This can significantly slow down performance, especially when the regex operation is called repeatedly in loops or in performance-critical sections. #### 2. Pros and Cons of Using Existing Regex: - **Pros**: - **Performance**: Reusing an existing regex object eliminates the overhead of object instantiation, leading to faster execution times, as demonstrated by the benchmark results. - **Less Memory Usage**: Creating fewer objects can lead to lower memory consumption. - **Cons**: - **Initial Complexity**: Requires more initial setup where the regex must be defined separately from where it is used. ### Benchmark Results In the benchmark results, we can observe the following performance metrics: - **"use existing regex"**: 14,546,399 executions per second. - **"instantiate new regex"**: 14,238,132 executions per second. The performance of the "use existing regex" approach is significantly better than that of instantiating a new regex, confirming the expectations around efficiency. ### General Considerations and Alternatives - **Alternative Approaches**: While using pre-compiled regular expressions is generally more efficient, there are other ways to handle string transformations in JavaScript, such as using native string methods or other parsing libraries like `lodash` or `underscore.js`, which might offer specific functions targeting string manipulation or pattern matching more efficiently or with additional features. - **Library Consideration**: In this benchmark, no external libraries are used, but libraries that help streamline regex operations (like validation libraries) may be beneficial for specific use cases, although they may also introduce additional overhead. In conclusion, the benchmark decisively highlights that reusing regex objects can yield significant performance benefits in JavaScript compared to creating new instances each time, a vital consideration for optimizing string manipulation tasks in performance-sensitive contexts.
Related benchmarks:
string replaceAll test
string.replace vs regexp.matchAll vs regexp.exec
sa sad test
split.includes vs regex boundary
RegEx.test vs. String.includes vs. String.match vs String.replace
RegEx.test vs. String.includes vs. String.match vs String.replace vs String.replace classic
rgx replace with if
how fast is regex2?
how fast is regex3?
Comments
Confirm delete:
Do you really want to delete benchmark?