Disclaimer: my question is not focused on the exercise, this is just an example (although if you have interesting tips on the example itself, feel free to share!).
Let's say I'm working on parsing some lines using Regex in JavaScript, and the main focus is performance (speed).
I have a part of a regular expression that checks a numeric string and then parses it using Number
if it is numeric:
if (/^\[[0-9]+]$/.test(str)) {
val = Number(str.match(/^\[([0-9]+)$/)[1]);
}
Note that in the conditional test there is no capture group around the numbers. This results in writing out basically the same regular expression twice, with the exception of the capture group a second time.
What I would like to know is; Does the capture group add to the regular expression used next to test()
, in a state that affects performance in some way? I would just like to use capture regular expression in both places until I could achieve performance.
And to the question why I do test()
, then match()
, and not match()
checking null
; I want to continue parsing as quickly as possible when there is a miss, but still a little slower when there is a hit.
If this is not clear from the above, I mean the JavaScript regex engine - although if it is different from the engines, it would be nice to find out too. I work specifically in Node.js here if it is also different between JS engines.
Thanks in advance!