Bug 152578

Summary: RegExp with anchor negation and grouping performance
Product: WebKit Reporter: shahar.soel
Component: JavaScriptCoreAssignee: Nobody <webkit-unassigned>
Status: NEW ---    
Severity: Normal CC: barraclough, ggaren, msaboff, org.webkit
Priority: P2    
Version: Safari 9   
Hardware: Mac   
OS: OS X 10.10   

Description shahar.soel 2015-12-28 16:41:19 PST
reproduced here:

https://jsperf.com/regexp-negation-in-group/5

Expected: 
* Similar performance for these two regExps
* Performance for these RegExp.test should be independent of input size as they anchor to the start of the input. 
var noGroup = /^"[^\\"]+"/;
var oneGroup = /^"(:?[^\\"])+"/;

Actual:
* for oneGroup regExp the performance is much worse.
anywhere from x30 slower for a single line input
to 5 orders of magnitude for a 10000 lines input.

When running the benchmark on other browsers (chrome/firefox)
The input size does not affect the performance.

Thanks.
Shahar.
Comment 1 shahar.soel 2015-12-30 07:28:58 PST
fixed the jsperf example (was using wrong syntax for none capturing groups)
http://jsperf.com/regexp-negation-in-group/6
Comment 2 shahar.soel 2016-08-08 00:35:49 PDT
JSPerf is down so I've added a reproducing jsfiddle instead.

https://jsfiddle.net/cbepLyum/2/

(open the console to see the results).
Comment 3 Pierre-Yves GĂ©rardy 2020-02-12 06:02:05 PST
There is still a slowdown when using groups, but it is now by a constant factor of ~4, rather than dependent on the subject length.

Surprisingly, the RegExp with two non-capturing groups is 10-20% faster than the one with only one.