Sam Stone
07/20/2023, 8:39 PMfun foo(userId: Int) {
doWork()
val user = loadFromDb(userId)
doMoreWork()
//faster to refer to user.id or userId?
}
Casey Brooks
07/20/2023, 8:52 PMRafs
07/20/2023, 9:11 PMbar(userId:Int)
. From somewhere within your application, you could call them as
foo(userId)
bar(userId)
and won't have to worry about which one is faster, but which one is more elegant and readable.Klitos Kyriacou
07/21/2023, 8:04 AMuserId
is a primitive value taken from the stack, and that's very fast. The JIT will probably do further optimizations, such as possibly keeping it in a CPU register. On the other hand, user.Id
involves possibly checking that user
is not null, then calling user.getId()
(a function call and a value access via indirection). However, even with this the JIT may optimize it.Sam Stone
07/21/2023, 1:53 PM