0

There are some rumors which claims that var is more performant than val in context of declaring a variable in kotlin since it does not have and does not need a immutability mechanism. I could not find any resources which only focuses to "declaring a variable" part while many of them focusing multithreading and they generally end up with "val is more performant than var since the compiler does not need to check that field is changed before accessing it". So what are your opinions ?

Sevban Bayır
  • 196
  • 3
  • 13
  • 1
    The runtime can potentially optimize performance when accessing the same property many times in a loop and it keeps returning the same thing. This is regardless of whether it's a val or var and would depend only on whether the setter of the var is actually being used in the middle of this loop. – Tenfour04 Jun 07 '23 at 13:25
  • Yeah, that makes sense thank you. – Sevban Bayır Jun 07 '23 at 15:12
  • “So what are your opinions?” — We don't want opinions here, we want facts :-) – gidds Jun 18 '23 at 20:12

3 Answers3

3

In case of running Kotlin code on JVM, you can face some optimizations for immutable variables declared with val. But you should never consider using one over another according to performance.

By using val when declaring class property without specifying any access modifiers, you basically tell compiler to generate "getter" method. In case of using var you tell it to generate "getter" and "setter", which means more code (you won't see it in a class body, it will be present in compiled code).

val and var are about design, you want to use val as mush as possible since immutability is highly preferred over mutability. Even if var was "faster" in terms of performance, you would drastically decrease code quality by replacing val with var everywhere.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Steyrix
  • 2,796
  • 1
  • 9
  • 23
  • Yeah, i absolutely agree with that. But, i just wanted to know that if there was a possibility to var is being faster than val what would be the cause of it. – Sevban Bayır Jun 07 '23 at 12:11
  • @SevbanBayır I don't know exactly, but it is possibly easier for the runtime to manage `var` since they are not as sensitive and strict as `val` – Steyrix Jun 08 '23 at 13:49
1

It is better to stick to the proper design principles (what is more correct according to the logic) and readability here, meanwhile there are some Kotlin guides recommending choosing val over var where it is possible, and suggesting that Kotlin is "val-first" language which has a subtle impact on code design itself.

Kotlin's val is similar to final keyword in Java. Taking into account the tight connection and compatibility between Java and Kotlin, take a look at this simple performance test: https://www.baeldung.com/java-final-performance.

I.e. applying the final keyword to variables (assuming val in our case) can have a minor positive impact on performance. One shouldn't expect any significant performance boost in general, but it is kind of justification for these "rumors".

grey
  • 31
  • 1
  • 5
0

I would agree with "val is more performat since the compiler knows it won't change before accessing it".

Imo, for real-case uses it won't affect much your performances if you choose var or val, you should use the correct variable declaration the context needs. I would never use (and the IDE usually warns you about this) a var for a variable if it's value won't change like for a constant. Don't worry about performances, it's not this what could potentally reduce your app performances.

Use the correct variable declaration for what you need, just this.

Happy coding!

ItzDavi
  • 443
  • 4
  • 13