Performance of instanceof with interfaces is multiple times slower than with classes

Christoph Dreis christoph.dreis at
Wed Jul 15 09:08:42 UTC 2020


please forgive me if this is a stupid question or a known problem.

I was working on something that involved an instanceof check and needed to change it slightly.
I was surprised to see that the performance difference between an interface and a class is quite big.

E.g. consider the following benchmark:

public class MyBenchmark {

	interface TestInterface {}
	private static class TestClass {}
	private static class AnotherClass {}

	public static class BenchmarkState {
		private Object clazz = new MyBenchmark.TestClass();

	public boolean testInstanceOfInterface(BenchmarkState state) {
		return state.clazz instanceof TestInterface;

	public boolean testInstanceOfClass(BenchmarkState state) {
		return state.clazz instanceof AnotherClass;

Benchmark                                                Mode  Cnt   Score    Error   Units
MyBenchmark.testInstanceOfClass                          avgt   10   2,085 ±  0,179   ns/op
MyBenchmark.testInstanceOfInterface                      avgt   10  18,783 ±  0,595   ns/op

I was surprised to see that the interface variant is so much slower.
Both checks should return false and there is no big hierarchy that needs to be walked up/down.

Could you enlighten me what the cause for this is and maybe point me to the code where this is done?
Is this maybe even a bug/regression? Can we maybe do something to improve the interface case?

Thanks in advance,

More information about the hotspot-runtime-dev mailing list