Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Regexp vs regexp precompiled
(version: 1)
Comparing performance of:
inline vs precompiled
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
const regex = /^TC/
Tests:
inline
"TP-asd".replace(/^TC/, "")
precompiled
"TP-asd".replace(regex, "")
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
inline
precompiled
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36 Edg/145.0.0.0
Browser/OS:
Chrome 145 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
inline
41085892.0 Ops/sec
precompiled
42710264.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark provided compares two approaches to using regular expressions (regex) in JavaScript: **inline regex creation** versus **precompiled regex**. This is relevant for performance optimization, especially in scenarios where regex operations are frequent. ### Options Compared 1. **Inline Regex Creation**: - **Test Case**: `"TP-asd".replace(/^TC/, "")` - The regular expression is defined directly within the `.replace()` method call. - **Pros**: - Simplicity: Easy to read and understand since the regex is directly associated with the operation. - Good for one-off usage where the regex is not reused. - **Cons**: - Performance Overhead: Each time this line is executed, the regex must be recompiled, which can slow down operations if invoked repeatedly in a loop or a large data set. 2. **Precompiled Regex**: - **Test Case**: `"TP-asd".replace(regex, "")` where `regex` is defined once beforehand as `const regex = /^TC/`. - **Pros**: - Efficiency: The regex is compiled once and can be reused across multiple invocations, reducing overhead during execution. - Optimal for repeated use: If the same regex is used multiple times, this approach leads to better performance. - **Cons**: - Slightly more setup: Requires defining the regex separately from the operation, which might introduce minor complexity. ### Benchmark Result Summary The benchmark results show a significant difference in performance between the two approaches: - **Inline**: 18,135,520 executions per second - **Precompiled**: 5,879,532 executions per second These results indicate that the inline approach is considerably faster in this scenario. With the inline regex being compiled each time, it's optimized for the specific use case here by allowing for quicker string manipulations, even though in other scenarios, precompiled regex would usually outperform the inline variant over many operations. ### Library and JavaScript Features No external libraries are being utilized in the benchmarks; rather, this test solely leverages JavaScript's built-in regex capabilities. The feature being tested is JavaScript's **regular expression (regex)** syntax, which is utilized to perform pattern matching and string manipulations efficiently. The regex pattern `/^TC/` specifically checks whether the string starts with the letters "TC." ### Other Alternatives 1. **Using Functions**: If a regex operation is conducted multiple times on varying inputs, encapsulating it within a function can also be a viable alternative. This abstracts away the complexity while still benefiting from either inline or precompiled patterns. 2. **Performance Optimization Techniques**: - **Memoization**: Caching results of computations when the input is known can reduce the need for repeated regex evaluations. - **String Handling Libraries**: Libraries like Lodash provide utility methods for string manipulations which could be beneficial but may not significantly outperform regex in this context. 3. **Using Typed Arrays or Other Data Structures**: For scenarios where the performance of string operations is paramount, exploring the use of different data structures to minimize string manipulation overhead could be another avenue worth investigating. In conclusion, while the choice between inline and precompiled regex can seem minor, it can lead to significant performance implications depending on the operational context and frequency of function calls in JavaScript.
Related benchmarks:
RegEx.test vs String.includes
RegEx.test vs. String.includes vs. String.match - 1
RegEx.test vs. String.includes incasesensitive
Reuse Regex? RegEx.test vs. String.match vs. String.search
RegEx.test vs. String.includes vs. String.match(Regex)
RegEx.test (with inline regex) vs. String.includes vs. String.match
Long regex test vs string includes
Longer regex test vs string includes
RegEx.test vs. Inline RegEx.test
Comments
Confirm delete:
Do you really want to delete benchmark?