Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The performance impact of using instanceof in Java

I am working on an application and one design approach involves extremely heavy use of the instanceof operator. While I know that OO design generally tries to avoid using instanceof, that is a different story and this question is purely related to performance. I was wondering if there is any performance impact? Is is just as fast as ==?

For example, I have a base class with 10 subclasses. In a single function that takes the base class, I do checks for if the class is an instance of the subclass and carry out some routine.

One of the other ways I thought of solving it was to use a "type id" integer primitive instead, and use a bitmask to represent categories of the subclasses, and then just do a bit mask comparison of the subclasses "type id" to a constant mask representing the category.

Is instanceof somehow optimized by the JVM to be faster than that? I want to stick to Java but the performance of the app is critical. It would be cool if someone that has been down this road before could offer some advice. Am I nitpicking too much or focusing on the wrong thing to optimize?

like image 527
Josh Avatar asked Sep 19 '08 16:09

Josh


People also ask

Is it good to use Instanceof in Java?

Probably most of you have already heard that using “instanceof” is a code smell and it is considered as a bad practice. While there is nothing wrong in it and may be required at certain times, but the good design would avoid having to use this keyword.

What is the advantage of using Instanceof?

Downcasting is one of the advantages of using instanceof operator. Downcasting is a process to hold object of parent class in child class reference object. It is reverse of upcasting, in which object of child is assigned to parent class reference object.

Why do we use Instanceof in Java?

The instanceof operator in Java is used to check whether an object is an instance of a particular class or not. objectName instanceOf className; Here, if objectName is an instance of className , the operator returns true . Otherwise, it returns false .

What is the significance of using instance of operator and getClass?

Coming to the point, the key difference between them is that getClass() only returns true if the object is actually an instance of the specified class but an instanceof operator can return true even if the object is a subclass of a specified class or interface in Java.


1 Answers

Approach

I wrote a benchmark program to evaluate different implementations:

  1. instanceof implementation (as reference)
  2. object-orientated via an abstract class and @Override a test method
  3. using an own type implementation
  4. getClass() == _.class implementation

I used jmh to run the benchmark with 100 warmup calls, 1000 iterations under measuring, and with 10 forks. So each option was measured with 10 000 times, which takes 12:18:57 to run the whole benchmark on my MacBook Pro with macOS 10.12.4 and Java 1.8. The benchmark measures the average time of each option. For more details see my implementation on GitHub.

For the sake of completeness: There is a previous version of this answer and my benchmark.

Results

 | Operation  | Runtime in nanoseconds per operation | Relative to instanceof | |------------|--------------------------------------|------------------------| | INSTANCEOF | 39,598 ± 0,022 ns/op                 | 100,00 %               | | GETCLASS   | 39,687 ± 0,021 ns/op                 | 100,22 %               | | TYPE       | 46,295 ± 0,026 ns/op                 | 116,91 %               | | OO         | 48,078 ± 0,026 ns/op                 | 121,42 %               | 

tl;dr

In Java 1.8 instanceof is the fastest approach, although getClass() is very close.

like image 185
Michael Dorner Avatar answered Sep 23 '22 13:09

Michael Dorner