r/ruby • • 23d ago

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

4 comments sorted by

View all comments

Show parent comment

1

u/sshaw_ 19d ago

Some good ol' fashion pointer incrementing. Nice!