performance-wise, better a `() -> Unit` or a `R...
# announcements
e
performance-wise, better a
() -> Unit
or a
Runnable
(on pure kotlin)?
s
why would there be a difference
e
I dont know, but maybe some the underlying implementation differences may have an effect
c
Lambdas are just compiled to normal classes with random names.
Runnable
is just an interface, so anytime you pass a Runnable you’re creating a class for that too. They are identical when used, so you should prefer using the lambda syntax for greater clarity and parity with other Kotlin code
💯 1
e
I had a guess that
Runnable
could benefit from some jvm optimizations.. anyway, thanks!
s
Everything compiled for the JVM is subject to optimizations potentially made by the particular implementation you are using
Many of these “micro-optimizations” are either not really an optimization when it comes down to the bytecode or are already handled for you by, say, HotSpot for example
👍 2
c
If anything, the lambdas are more efficient since they can be inlined into
inline
functions, and thus execute without needing to create that new lambda class. I’m not sure if
Runnables
are ever inlined in the same way
s