Ruby casecmp vs downcase
pro-tip that I learned today:
when comparing case-insensitive strings, this can be 1.2x to 2x faster:
str = "TEST"
str.casecmp?("test")than this:
str = "TEST"
str.downcase == "test"Caveat: non-ASCII/Unicode strings can behave differently with downcase vs casecmp
9
Upvotes
View all comments
Show parent comment
1
u/h0rst_ 21d ago edited 21d ago
This appears to be fixed in Ruby 4.1, I'm seeing very similar results for both
casecmpversions:In 4.0 the
casecmp?version is still roughly 1.6 times slower.It looks like it's this commit.