const
KT-P1001In Kotlin, using const
properties is preferred over regular properties for improved performance, efficiency, code clarity, and compile-time safety. const
properties are evaluated at compile-time, leading to faster execution and eliminating the need for runtime initialization. const
properties also enhance code readability and consistency by
forEach
with ranges KT-P1000Using the forEach
method on ranges has a heavy performance cost. Prefer using simple for
loops.
String
KT-P1002When converting primitive types to String
, you should avoid creating an object of the respective primitive type first and then using .toString()
for converting it to a string. Creating a temporary object is an unnecessary overhead and can incur a performance penalty.
In most cases using a spread operator causes a full copy of the array to be created before calling a method. This has a very high performance penalty. The Kotlin compiler since v1.1.60 has an optimization that skips the array copy when an array constructor function is used
Array<Primitive>
KT-P1003In Kotlin, using Array<Primitive>
can lead to implicit boxing, which can reduce performance. Kotlin provides specialized array classes for each primitive type (e.g., IntArray
, DoubleArray
, BooleanArray
, etc.), which offer better performance and avoid unnecessary boxing and unboxing operations.