One remark though on your benchmark, I think that compile time of regexes is not excluded from the timing, which would be fair as if done correctly it will only impact the first execution of your code.
I'm saying that but I'm totally unsure of how JS handles this :)
Oh that's a really good point! I'll check that out.
So I added a few new tests:
*The RegExp instance is declared outside the test loop.
I'm seeing that #1 is significantly faster than the String.match() test where the regular expression is provided inline. The difference seems to be (very roughly) in the magnitude of 4,769,893 ops/sec.
The difference between #2 and #3 is significantly smaller and in favor of the inline test. The (very) rough difference seems to be around 121,562 ops/sec.
So, perhaps the String.match() method has some optimizations for handling inline regular expressions that aren't bogged down by the usage of an explicit RegExp instance.
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.