Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a performance penalty using capture groups in RegExp#test?

Disclaimer: my question is not focused on the exercise, it's just an example (although if you have any interesting tips on the example itself, feel free to share!).

Say I'm working with parsing some strings with Regex in JavaScript, and my main focus is performance (speed).

I have a piece of regex which checks for a numeric string, and then parses it using Number if it's numeric:

if (/^\[[0-9]+]$/.test(str)) {
  val = Number(str.match(/^\[([0-9]+)$/)[1]);
}

Note how the conditional test does not have a capture group around the digits. This leads to writing out basically the same regex twice, except with a capture group the second time.

What I would like to know is this; does adding a capture group to a regex used alongside test() in a condition affect performance in any way? I'd like to simply use the capture regex in both places, as long as there is no performance hit.

And to the question as why I'm doing test() then match() rather than match() and checking null; I want to keep parsing as fast as possible when there's a miss, but it's ok to be a little slower when there's a hit.

If it's not clear from the above, I'm referring to JavaScript's regex engine - although if this differs across engines it'd be nice to know too. I'm working specifically in Node.js here, should it also differ across JS engines.

Thanks in advance!

like image 548
whitfin Avatar asked Nov 09 '22 02:11

whitfin


1 Answers

Doing 2 regexps - that are very similar in scope - will almost always be slower than doing a single one because regexps are greedy (that means that they will try to match as much as they can, usually meaning take the maximum amount of time possible).

What you're asking is basically: is the cost of fewer memory in the worst case scenario (aka using the .test to save on memory from capture) faster than just using the extra memory? The answer is no, using extra memory speeds up your process.

Don't take my word for it though, here's a jsperf: http://jsperf.com/regex-perf-numbers

like image 117
Stefan Avatar answered Nov 14 '22 21:11

Stefan