Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
capturing and non capturing regex
(version: 1)
Comparing performance of:
capturing vs non capturing
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
let string = 'hello123' let f = /(hello|hi)123/ let n = /(?:hello|hi)123/
Tests:
capturing
string.match(f)
non capturing
string.match(n)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
capturing
non capturing
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
capturing
15344823.0 Ops/sec
non capturing
16613423.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
This benchmark compares the performance of two types of regular expressions in JavaScript: capturing and non-capturing groups. Both types of regex are commonly used for pattern matching in strings, but they serve slightly different purposes. ### Benchmark Overview #### Script Preparation ```javascript let string = 'hello123'; let f = /(hello|hi)123/; // Capturing regex let n = /(?:hello|hi)123/; // Non-capturing regex ``` Here, the benchmark initializes a test string, `string`, with the value `'hello123'`. It also defines two regular expressions: 1. `f` which uses capturing groups `/(hello|hi)123/` 2. `n` which uses non-capturing groups `/(?:hello|hi)123/` ### Individual Test Cases 1. **Capturing Test Case**: - **Definition**: `string.match(f)` - **Purpose**: This regex captures the substring `hello` or `hi` in `string`. Capturing groups are useful when you need to extract and use the matched portions later. 2. **Non-Capturing Test Case**: - **Definition**: `string.match(n)` - **Purpose**: This regex behaves similarly but does not store the matching parts. Non-capturing groups are used when you want to apply a group for quantifiers or logical conditions without needing to extract the captured content. ### Performance Results The benchmark results indicate the number of executions per second for both test cases: - **Non-Capturing**: 16,613,423 executions per second - **Capturing**: 15,344,823 executions per second The non-capturing regex performed faster than the capturing regex. This is generally expected, as non-capturing groups require less overhead since they do not need to store matches. ### Pros and Cons #### Capturing Groups - **Pros**: - Useful when you need to extract specific parts of the matched string for further processing. - Can be referenced within the same regex for more complex patterns. - **Cons**: - Performance overhead due to the need to maintain captured values. - Increased complexity in cases where captured values are not needed. #### Non-Capturing Groups - **Pros**: - More performant as they avoid storage for matches. - Simplifies regex functions where captured values are unnecessary. - **Cons**: - Cannot reference matched substrings later in the regex execution. - Limited usability if specific elements need to be extracted. ### Other Considerations - In scenarios involving complex regex patterns, the performance difference can become significant, making non-capturing groups a better option when extraction is not required. - Both regex types behave similarly in simpler patterns, so understanding when to use each type can help optimize performance and resource usage. ### Alternatives 1. **Using Different Regex Engines**: Various JavaScript libraries offer different regex engines (like XRegExp) that may provide enhanced functionality. These libraries allow for more complex operations, such as named capturing groups, but may trade off some performance. 2. **String Methods**: For simple substring searches, methods like `String.includes()`, `String.startsWith()`, or `String.endsWith()` may provide clarity and improved performance when regular expressions are not required. 3. **Custom Logic**: In some use cases, manual string manipulation combined with logical conditions might outperform even optimized regex, particularly for predictable formats. This benchmark highlights the performance characteristics of capturing vs. non-capturing regex in JavaScript, guiding engineers in their decision-making process when using regular expressions for pattern matching.
Related benchmarks:
String compare VS regex
booger
Regex vs Forloop Performance
RegEx.test vs RegEx.match when fails
RegEx.test vs. String.includes vs. String.match1
Demo benchmark fork - pre-initialized regex
starts with vs regex
r includes vs regex
starts/endswith vs regex.test
Comments
Confirm delete:
Do you really want to delete benchmark?