DEV Community

Discussion on: How CloudFlare could have avoided its outage (maybe)

Collapse
 
xowap profile image
Rémy 🤖

Thanks!

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 :)

Collapse
 
foresthoffman profile image
Forest Hoffman

Oh that's a really good point! I'll check that out.

Thread Thread
 
foresthoffman profile image
Forest Hoffman • Edited

So I added a few new tests:

  1. String.match() *
  2. RegExp.exec()
  3. RegExp.exec() *

*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-...