Wonder why standard library doesn’t have binarySea...
# stdlib
e
Wonder why standard library doesn’t have binarySearch variant for arrays?
c
I guess it's because we're not supposed to use arrays that much 🤔
e
I'm converting some java code that was written with the thought of the performance
c
d
Yeah, regular arrays are discouraged because they are mutable, lack proper equals/hashCode/toString, and aren't covariant. Immutable Arrays address all those issues and also outperform regular arrays for many operations.
k
If you're targeting JVM only, you can still use Java's
binarySearch
on arrays.
If you're targeting multiple platforms, you can use
Array<whatever>.asList()
which wraps the original array and doesn't create boxed versions of each element, so you get to keep the memory efficiency, cache locality etc.
d
@Klitos Kyriacou, using
asList()
on a primitive array auto-boxes the values each time they are accessed because all the list operations operate on generics. So the
asList()
operation itself doesn't auto-box anything but all subsequent operations will.
e
Thank you, people! I learned a lot
k
@Dan Rusu yes, I realise that, but it only boxes one element at a time, instead of all elements. Thus, you don't get the typical 3-4 times memory usage increase that you would get if you used
toList()
instead of
asList()
.