test: fix RegExp nits
Checklist
-
make -j4 test
(UNIX), orvcbuild test
(Windows) passes -
tests and/or benchmarks are included -
commit message follows commit guidelines
Affected core subsystem(s)
test
It will be easier to review this PR commit by commit, performance-wise and for better comprehending (different changes are confusingly interwoven in the overall diff).
-
Remove redundant
RegExp
part.RegExp
part with a quantifier '0 or more times' is redundant if it is used at the very edge of theRegExp
and is not captured or does not affect match indices (UPD: and should not greedily take up previous similar parts). -
Remove needless
RegExp
flag.In fixed case,
/g
flag is needless in the boolean context. -
Remove needless
RegExp
capturing.Use non-capturing grouping or remove capturing completely when:
- capturing is useless per se, e.g. in
test()
check; - captured groups are not used afterward at all;
- some of the later captured groups are not used afterward.
- capturing is useless per se, e.g. in
-
Use
test()
, notmatch()
orexec()
in boolean context.match()
andexec()
return a complicated object, unneeded in a boolean context. This fix also makes the code more clear and predictable. -
Do not needlessly repeat
RegExp
creation.This is the largest commit, however, it is rather easy to skim. It takes
RegExp
creation out of cycles and other repetitions.As long as the
RegExp
does not use/g
flag + match indices, we are safe here.In tests, this fix hardly gives a significant performance gain, but it increases clarity and maintainability, reassuring some
RegExp
s to be identical.RegExp
s in functions are not taken out of their functions: while these functions are called many times and their RegExps are recreated with each call, the performance gain in test cases does not seem to be worth decreasing function self-dependency.