*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.
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.
jsperf.com/slice-vs-startswith-vs-...