Open LeaVerou opened 3 years ago
Indeed it must be performance: I just wrote a quick benchmark and the regex version seems to be about 70% slower in every browser. OTOH we are talking about a roughly 15ms total difference in 1,300,000 comparisons, so still not sure it's worth it, but I can see the point now.
In jsep, I've seen this pattern a lot:
Is this done for performance? If not, it would be far easier to just do e.g.
/^[A-Z0-9]$/i.test(str)
instead of(ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122) || (ch >= 48 && ch <= 57)
, but I imagine there is a reason it wasn't done this way? We basically convert the string to a number before calling any of these functions, it's not like we even have the number in the first place. Could these be optimizations the compiler can already do with a well anchored regexp?