Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Unnecessary non-capturing groups
(version: 0)
https://stackoverflow.com/questions/45291700/javascript-regex-non-capture-faster-than-no-parenthesis-at-all
Comparing performance of:
unneededNonCaptureRE vs noNonCaptureRE vs anotherUnneededNonCapture vs anotherNoNonCapture vs indexOf
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var loremString = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum." var unneededNonCaptureRE = /(?:z)+/ var noNonCaptureRE = /z+/ var anotherUnneededNonCapture = /lab(?:o)rum/ var anotherNoNonCapture = /laborum/
Tests:
unneededNonCaptureRE
unneededNonCaptureRE.lastIndex = 0 unneededNonCaptureRE.exec(loremString)
noNonCaptureRE
noNonCaptureRE.lastIndex = 0 noNonCaptureRE.exec(loremString)
anotherUnneededNonCapture
anotherUnneededNonCapture.lastIndex = 0 anotherUnneededNonCapture.exec(loremString)
anotherNoNonCapture
anotherNoNonCapture.lastIndex = 0 anotherNoNonCapture.exec(loremString)
indexOf
loremString.indexOf('laborum')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
unneededNonCaptureRE
noNonCaptureRE
anotherUnneededNonCapture
anotherNoNonCapture
indexOf
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided benchmark measures the performance of JavaScript regular expression (regex) execution, specifically comparing different approaches to avoid unnecessary non-capturing groups. **What are non-capturing groups?** In regex, a capturing group is denoted by parentheses `()` and captures a portion of the input string. A non-capturing group is denoted by `(?:)` and does not capture any part of the input string. In this benchmark, we're comparing two approaches: using non-capturing groups (`(?:z)+`) versus capturing groups (`z+`). **Options being compared** The benchmark compares four options: 1. `unneededNonCaptureRE`: Uses a non-capturing group (`(?:z)+`) followed by an unnecessary `lastIndex = 0` and `exec()` call. 2. `noNonCaptureRE`: Uses a capturing group (`z+`) followed by an unnecessary `lastIndex = 0` and `exec()` call. 3. `anotherUnNeededNonCapture`: Similar to `unneededNonCaptureRE`, but with a slightly different regex pattern. 4. `anotherNoNonCapture`: Similar to `noNonCaptureRE`, but with another regex pattern. **Pros and Cons of each approach** 1. **`unneededNonCaptureRE`**: This option uses a non-capturing group, which can avoid some overhead associated with capturing groups. However, the unnecessary `lastIndex = 0` call may not have any significant impact on performance. 2. **`noNonCaptureRE`**: This option uses a capturing group, which is generally more efficient than non-capturing groups for simple regex patterns. The unnecessary `lastIndex = 0` call can still be beneficial. 3. **`anotherUnNeededNonCapture`** and **`anotherNoNonCapture`**: These options have similar pros and cons as the first two options, but with different regex patterns. **Library usage** The benchmark uses the built-in `exec()` method of RegExp objects to execute the regex patterns. No additional libraries are used. **Special JavaScript features or syntax** None are mentioned in the provided benchmark definition. **Other alternatives** Some alternative approaches could include: * Using a regular expression engine like v8 (used by Chrome) or SpiderMonkey (used by Firefox), which may offer better performance. * Optimizing the regex patterns for specific use cases, such as using character classes instead of capturing groups. * Using other optimization techniques, such as memoization or caching. Please note that this is a simplified explanation, and actual performance results may vary depending on the specific test environment and hardware.
Related benchmarks:
Unnecessary non-capturing groups
Unnecessary non-capturing groups
Unnecessary non-capturing groups
regex .replace() vs literal .replaceAll()
Comments
Confirm delete:
Do you really want to delete benchmark?