Tuesday, June 17, 2014

Implicit Performance Overhead of Using Java 8 Optional

A nice blog post by Edwin Dalorzo gives an essential guide to the Java 8 Optional objects. However, Optional as a value-based class implies each time an object is created to wrap the return result. This may kill the performance for a similar reason like autoboxing when a method returning an Optional result is invoked repeatedly. The following test cases evaluate the performance overhead.

Results on a desktop machine like the MacBook Air even show the significant performance hit, not to mention embedded devices such as Android.
  •  testNumberNull(): 1000 to 1400us
  •  testNumberOptional(): 2000 to 4000us or more
Use wisely!

No comments:

Post a Comment