javascript regex non-capture faster than no parenthesis at all

Viewed 400

I was playing with jsperf to see if chrome optimizes the javascript regular expression by removing unnecessary non-capturing groups. However I am left baffled with an unexpected result.

Test case

const anotherUnneededNonCapture = /lab(?:o)rum/
const anotherNoNonCapture = /laborum/

anotherUnneededNonCapture.exec(loremString)
anotherNoNonCapture.exec(loremString)

(I am testing against a lorem ipsum string where "laborum" is the last word)

Expectation

I expected to have both "exec" tests to have the same performance if chrome does optimize the RE by removing the unnecessary non-capture group. If the engine does not apply this optimization, i expected the normal string to be much faster, since it will not have the overhead of the unnecessary group.

Actual result

The expression with the "unneeded" non capture group is always almost 2x faster than its counterpart!

enter image description here

Link to the jsperf

https://jsperf.com/simple-quantifier-optimization-2/1

I also have tried other scenarios on different jsperf tests, all with similar results

any ideas? Thanks a lot, Cheers!

1 Answers
Related