Javascript regex does not capture faster than no parentheses

I played around with jsperf to make sure chrome optimizes the javascript regex by removing unneeded non-capturing groups. However, I am left bewildered by an unexpected result.

Test version

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

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

      

(I am testing the lorem ipsum line where "labum" is the last word)

Expectation

I expected both "exec" tests to have the same performance if chrome optimizes the RE by removing the unnecessary group without capturing. If the engine does not apply this optimization, I would expect a normal string to be much faster as it would not have the unnecessary group overhead.

Actual result

An expression with an "unnecessary" group without capturing is always almost 2 times faster than its copy!

enter image description here

Link to jsperf

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

I have also tried other scripts for different jsperf tests, all with similar results

any ideas? Thanks a lot, Cheers!

+3


source to share


1 answer


As of V8, the reason for the difference has to do with the "optimization" that occurs when the regex literal is just a string. In these cases, a simple search for strings (for example, implicit loremString.indexOf('laborum')

) is performed , whereas this change in behavior is skipped if there are special regex characters in the regex string.

So it /lab(?:o)rum/

really goes through the regexp mechanism and /laborum/

implicitly goes through the search indexOf()

.



This can be seen when profiling code from node with --prof

and then with command line parameters --prof-process

.

+2


source







All Articles