Ahmad Bamieh Ahmad Bamieh - 3 years ago 118
Javascript Question

javascript regex non-capture faster than no parenthesis at all

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!

Answer Source

As far as V8 goes, the reason for the difference has to do with an "optimization" that occurs when the regexp literal is just a plain string. In those cases, a simple string search is done (e.g. an implicit loremString.indexOf('laborum')) whereas this change in behavior is skipped if there are any special regexp characters in the regexp string.

So /lab(?:o)rum/ is actually going through the regexp engine and /laborum/ implicitly goes through an indexOf()-like search.

You can see this when profiling the code from node with the --prof and then afterward with --prof-process command-line options.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download