noob
noob

Reputation: 9202

regex ?: performance

What's the difference between this regex: /(everything|cool)/gi and this one: /(?:everything|cool)/gi ?

I'm asking this because I've got an regex which I wasn't able to write myself* and there are, as you can see below, a lot of ?: in that regex. I've read somewhere that ?: is bad for performance so I want to remove it.Can I remove it or is it important for anything?

* (?:(?=(?:(?:\\.|"(?:\\.|[^"\\])*"|[^\\'"])*'(?:\\.|"(?:\\.|[^"'\\])*"|[^\\'])*')*(?:\\.|"(?:\\.|[^"\\])*"|[^\\'])*$)(?=(?:(?:\\.|'(?:\\.|[^'\\])*'|[^\\'"])*"(?:\\.|'(?:\\.|[^'"\\])*'|[^\\"])*")*(?:\\.|'(?:\\.|[^'\\])*'|[^\\"])*$)(?:\\.|[^\\'"]))+

Upvotes: 0

Views: 602

Answers (3)

Roland Illig
Roland Illig

Reputation: 41617

You should have heard that (foo) is slower than (?:foo). This is because the first one is a capturing group, and the second one is a non-capturing group. the second one has less work to do (it doesn't need to remember the text that matched), so it should be faster.

Upvotes: 2

Rob W
Rob W

Reputation: 348972

Without ?:, a reference to a matched group is created.
With a ?:, the group is matched, but not captured.

Here's a benchmark on both methods: http://jsperf.com/regex-capture-vs-non-capture

By looking at the bars, one would say that the non-captured groups are faster. However, if you look at the bottom, the differences can be neglected, since both methods are already incredibly fast.

Removing or adding ?: to an existing solution might break the code, so I advise to not edit the RegExp when it's not causing any issues.

Upvotes: 4

cHao
cHao

Reputation: 86506

(?:...) is fine. It's when capturing groups, and particularly backreferences to them, get involved that you start seeing performance hits.

Upvotes: 4

Related Questions