Kotlin vs Java: performance

How does Kotlin performance compare to Java’s?

One of the questions you’re bound to have when switching over to Kotlin is whether or not it is faster than Java.

While an application’s performance is highly dependent on its OS, runtime, and the actual code, in general Kotlin will perform very similarly to Java.

JetBrain’s kotlin compiler produces JVM bytecode.  While I’m not going to an analysis of the bytecode that it produces, from the research I’ve done it appears that it is very similar (at least in the performance sense) to the bytecode that Java compiles to.

But…

While the question posed in this blog post is definitely a common one, it’s a bit too simplistic.  Much of how an app performs is dependent on the actual code that you write and the algorithms and data structures that you choose to use.

Kotlin has a distinct advantage over Java in this arena, because it provides us with implementations of certain programming patterns.  These patterns are highly efficient and likely to be less buggy than if you were to roll your own.

Take Kotlin Data Classes for example

You get this construct for free in Kotlin.  In order to do the same thing in Java, you have to write all the boilerplate code yourself.   This introduces the potential for that code to be buggy and inefficient.  Not good.

Joshua Bloch’s Effective Java describes the Immutable Object pattern.  Kotlin Data Classes are a great implementation of this pattern.  And you get it for free, built into the language.

Conclusion

Comparing Kotlin’s performance to Java’s is a common question that many have before switching from Java to Kotlin.  Kotlin compiles to JVM bytecode and the bytecode it produces is very similar to Java’s.  There is no reason that Kotlin should be slower than Java when comparing equivalent code.

Comparing speed of languages is less important than the data structures and algorithms used by the developer.  Kotlin provides some great built-in patterns that result in cleaner, easier to read, and less buggy code.