does anyone know if `b in a..c` has the same perfo...
# getting-started
c
does anyone know if
b in a..c
has the same performance as
a <= b && b <= c
?
s
I think it gets optimized into the same bytecode
c
thank you
i would believe so
c
oh i see, i would assume it also works for Chars right?
'0'..'9'
i would use the
Char.isDigit()
method but apparently it includes other numeric characters from different languages
e
also any Comparable in a different optimization, which of course just eliminates the range, as it still needs the compare() function call
c
thanks a lot 👍
k
Now that one was easy to optimize. How about
c in ('a'..'z') + ('A'..'Z') + ('0'..'9')
?
e
no, that results in a
List<Char>
and
.contains
c
oh i see. so it's better if you do
c in 'a'..'z' || c in 'A'..'Z'
i suppose?
s
I'm honestly not sure, and I think this starts to get into preoptimization territory -- for 99% of applications it doesn't matter. In any case, I think you could use
Char.isLetter()
(and pass in a Locale if that really matters to you).
k
How do we pass in a Locale?
s
isLetter
has an optional Locale parameter, just pass it in
k
s
Maybe it's in Java's
Character
? idk
🙂 1
r
If such optimization really matters I would at least encapsulate it in an inline extension
⬆️ 1