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/
unneededNonCaptureRE.lastIndex = 0
unneededNonCaptureRE.exec(loremString)
noNonCaptureRE.lastIndex = 0
noNonCaptureRE.exec(loremString)
anotherUnneededNonCapture.lastIndex = 0
anotherUnneededNonCapture.exec(loremString)
anotherNoNonCapture.lastIndex = 0
anotherNoNonCapture.exec(loremString)
loremString.indexOf('laborum')
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
unneededNonCaptureRE | |
noNonCaptureRE | |
anotherUnneededNonCapture | |
anotherNoNonCapture | |
indexOf |
Test name | Executions per second |
---|---|
unneededNonCaptureRE | 2006443.1 Ops/sec |
noNonCaptureRE | 1979347.8 Ops/sec |
anotherUnneededNonCapture | 2615443.2 Ops/sec |
anotherNoNonCapture | 2086477.8 Ops/sec |
indexOf | 11609892.0 Ops/sec |
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:
unneededNonCaptureRE
: Uses a non-capturing group ((?:z)+
) followed by an unnecessary lastIndex = 0
and exec()
call.noNonCaptureRE
: Uses a capturing group (z+
) followed by an unnecessary lastIndex = 0
and exec()
call.anotherUnNeededNonCapture
: Similar to unneededNonCaptureRE
, but with a slightly different regex pattern.anotherNoNonCapture
: Similar to noNonCaptureRE
, but with another regex pattern.Pros and Cons of each approach
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.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.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:
Please note that this is a simplified explanation, and actual performance results may vary depending on the specific test environment and hardware.