Is there any public benchmark of `typeOf` (1.8+, p...
# codereview
m
Is there any public benchmark of
typeOf
(1.8+, preferable)? I know it will be slower than a direct access to
Type::class
but how much slow will it be? 🤔 Reference: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.reflect/type-of.html
g
But why would you need typeOf if you already have access Type::class?
typeOf is useful when you need KType with generic information. So even if it 2-3 magnitude slower you not always can replace it If your code is performance sensitive you probably want to measure it yourself for your own case
m
Thank you for your answer @gildor! To be fair, my question is more for learning purposes than a real use case - but I was envisioning retaining generic info as you described. Just for matter of example, let's say we are developing a KMP DI/SL which the type passed in can have generic info (but not always), and the DI/SL would return an instance. Normally, the approach would be to use a KClass to find the correct instance provider but with KType we could retain the generic info and differentiate same class with different generics without custom qualifiers - which would be really nice to have. On the other hand, a DI/SL could map a really big quantity of classes, so I wonder if the performance impact would be problematic and I was wondering if JetBrains would have benchmarks for typeOf. An alternative use would be to check if there is
typeParameters
and deciding by using KClass or KType based on the need. Not sure how better that would be. Does that make any sense?
g
If I would like to differentiate by generic on level of DI, I would use KType, otherwise KClass, even if performance-wise it’s similar At least it would be more explicit
m
Thank you for taking the time to answer, I super appreciate. It makes sense. 🤗
a
fair enough 👍 Perhaps by having secondary API for generics call, wouldn’t impact the “core” features performances but we would open to things for generics