1

What is considered best practice, and what is the fastest to compute?

[A-Za-z]

vs.

[a-z]/i

Assuming that you don't care about the rest of the regex's case. I'm wanting to know with exactly those 2 regex's which is the fastest. Or does it yield the same outcome under the hood.

Marais Rossouw
  • 937
  • 1
  • 10
  • 29
  • 4
    Performance might vary from browser to browser - have you tried some test cases at https://jsperf.com/ (or wherever)? As for best practice, that's probably a matter of opinion, but I much prefer the `/i` version because longer regexes get harder and harder to read if you have to keep repeating `A-Za-z` throughout. – nnnnnn Nov 01 '16 at 03:30

1 Answers1

3

A quick test on jsperf.com, shows that to search a single string from a-z on a sample string would results that [a-zA-Z] is slightly faster.

Here are the tests that I performed.

choz
  • 17,242
  • 4
  • 53
  • 73
  • "slightly faster" --- for me the "slightly" was less than 0.5%. Does it even count as a difference? – zerkms Nov 01 '16 at 03:42
  • @zerkms It's only visible if I test it globally and run it multiple times. Otherwise, I would've said that they're in equal speed. I am still trying to improve my answer though since this thing might vary if I run it from my node server. – choz Nov 01 '16 at 03:46
  • https://jsperf.com/regex-perf-test-case-insensative Thanks for that, turns out `/[A-Z]/i` is the faster.. But not by much... Would interesting to see what some absolute expert regex guru's have to say however. – Marais Rossouw Nov 01 '16 at 03:46
  • 2
    @MaraisRossouw an absolute guru would say: "you're wasting your time optimising something irrelevant" – zerkms Nov 01 '16 at 03:47
  • 1
    Yeah exactly... But still something nice to know for the sake of just wanting to know... Just like `++x is` faster than `x++`. @zerkms – Marais Rossouw Nov 01 '16 at 03:49
  • 3
    @MaraisRossouw "Just like ++x is faster than x++" --- well, that simply is not true. Which just proves that this kind of "knowledge" is useless. – zerkms Nov 01 '16 at 03:49
  • @MaraisRossouw Wait.. Did you actually test that? since thats a new thing to me.. and I woulda assumed they shoulda been the same thing. – choz Nov 01 '16 at 03:49
  • 2
    I would actually call into question the accuracy of which is faster... it's negligible if anything. Depending on the order in which the tests are run can have an outcome, what might be cached, etc. Both patterns perform the same exact amount of steps — that's what you should be looking for. – l'L'l Nov 01 '16 at 03:51
  • I didnt no, but this guy did (http://stackoverflow.com/questions/1812990/incrementing-in-c-when-to-use-x-or-x). @choz – Marais Rossouw Nov 01 '16 at 03:51
  • @l'L'l right? So does the same "number" of steps mean the same number of same steps? Or same number, but different steps? I mean surely having the same number of steps, but having different steps the speed could vary? – Marais Rossouw Nov 01 '16 at 03:52
  • @MaraisRossouw I think your `faster` term is how early `x` gets mutated before the next operation. – choz Nov 01 '16 at 03:52