title: The performance impact of using instanceof in Java
body: <p>I am working on an application and one design approach involves extremely heavy use of the <code>instanceof</code> operator. While I know that OO design generally tries to avoid using <code>instanceof</code>, 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 <code>==</code>?</p>
  
  <p>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. </p>
  
  <p>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.</p>
  
  <p>Is <code>instanceof</code> 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?</p>