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)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
unneededNonCaptureRE | |
noNonCaptureRE | |
anotherUnneededNonCapture | |
anotherNoNonCapture |
Test name | Executions per second |
---|---|
unneededNonCaptureRE | 2084003.0 Ops/sec |
noNonCaptureRE | 2246493.8 Ops/sec |
anotherUnneededNonCapture | 3582986.2 Ops/sec |
anotherNoNonCapture | 2785470.2 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Definition
The benchmark is testing the performance of different regular expression (regex) patterns in JavaScript. The specific pattern being tested is related to capturing groups, which are used to capture parts of a string that match a certain pattern.
There are two main regex patterns being compared:
unneededNonCaptureRE
: This pattern uses a non-capturing group ((?:
) to avoid capturing the entire match.noNonCaptureRE
: This pattern does not use any grouping at all, it simply matches the string with the z
character.The benchmark is comparing the performance of these two patterns in different test cases:
anotherUnneededNonCapture
: This pattern uses a non-capturing group similar to unneededNonCaptureRE
, but with some additional characters.anotherNoNonCapture
: This pattern does not use any grouping, it simply matches the string with the laborum
substring.Options Compared
The benchmark is comparing the performance of two different approaches:
(?:
) in the regex pattern, the engine can avoid capturing the entire match and instead only capture the matched part.Pros and Cons
Library Used
The exec()
method used in the benchmark is a part of the JavaScript RegExp object. The RegExp object represents a regular expression pattern and provides methods for searching, matching, and replacing text.
In this case, the RegExp object is used with the exec()
method to execute the regex pattern on the input string loremString
. The lastIndex
property is used to reset the index of the last match before executing the regex again.
Special JS Features or Syntax
There are no special JavaScript features or syntax being tested in this benchmark. It's a straightforward test of the performance difference between using non-capturing groups and not using grouping in regular expressions.
Alternatives
If you're interested in alternative approaches, here are some options:
?
) to reduce the amount of work needed to process the regex.Keep in mind that these alternatives may not directly affect the performance difference between using non-capturing groups and not using grouping.