Let’s say I want to run three regex tests on the same dataset (in string form), and run a different code pending the result of each test. Now I am assuming that the default (JS) regex.test(s) loops through the set at least until it finds the sought value, at which point it can return and forget about the rest of the set.
So in the best case scenario, you have three matches, which entails three incomplete loops which is not so bad, O(3(n-k)) I believe. The worst case scenario, meaning no matches, would require three full loops through the set.
Now assuming we are only checking for single char values (whole words would be a little more involved), would it be more performant to write a single for loop, and within each iteration run three tests, regs.test(s[i]), leading to a worst case scenario of O(n)instead of O(3n) and a best case scenario of O(n-k)? Or does the (V8) engine automatically batch regex tests in the background, when possible?
I am practicing discussing time complexity to prepare for technical interviews, so any critique of my analyses is welcome. Thanks!