JavaFX - Performance Optimization
By Rakesh Menon on Feb 23, 2010
|There was a question related to Best Practices for speed optimization in Oracle-Mix forum. There are many small tips & tricks, just thought of listing out what ever came to my mind..|
Use Layout: For positioning node always use Layout. There are plenty of layout available such as HBox, VBox, Tile, Flow and Stack. If we need any custom layout you can use Container or Panel. Position node using layoutX and layoutY attributes. Use translateX and translateY for Animation.
Consolidate Theme Attributes : We can reuse many theme related objects such as Font, Color, Paint, Image. Eg: If we use same Font for many Label/Text, we can use the same Font instance instead of creating one each for each instance of Node. This consolidation also allow us to customize the UI. You may refer to Theme used in Calendar sample. Also share Image instances such as default-image, place-holder image etc across multiple objects.
Bind with Caution: I have stressed on this in one of my previous post. Repeating the same again. Use this mostly for wiring model to view. Minimize use for other purpose such as layout of Nodes. Also better to avoid chaining of binds, its very problematic and difficult to debug.
JavaFX for View: There is a tendency to write entire application using pure JavaFX. This may be fine for small widget like application or samples. But when you write real business application we need to push out the business logic, IO operations and non-ui related code to Java layer. Use JavaFX Async APIs + Callback methods so as to execute these code outside Event-Dispatch-Thread. HttpRequest API demonstrates use of this approach.
Reduce use of Effects: In many cases we may need to have static effects in UI, such as nice gradient for background, font effects etc. If these effects are static, its always better to use Image instead of effect API.
Stroke Vs Fill: Instead of using stroke attribute for shape, we may use nested shapes with fill attribute. Example: For creating a stroke for Rectangle, we can create two instances of Rectangle overlapping, with the inner Rectangle smaller than outer Rectangle. This will have better performance compared to use of stroke attribute.
Avoid Class-Path in Manifest: When we add various jars as library to Netbeans, these jars are directly referenced in Class-Path of Manifest file of the application jar. When deployed, this will force to download all jars before start of application. Concept of lazy download will not work. Also there will be an attempt to make an unnecessary network connection, even though resources such as plugin.jar, javaws.jar etc are not hosted. This will increase initial applet/javaws startup time.
Use Image.width & Image.height: If we need to show a thumbnail of large image, its better to specify its width and height Image object itself rather than resizing the ImageView using fitWidth and fitHeight attribute. This will reduce the memory used to hold the image.
Provide Startup Feedback: Many a times people get bored due to lack of proper feedback during startup. This creates a notion of increased startup time. Except for small use-cases such as advertisements, RIA's do have some startup time. It will be nice to provide a Custom Splash Screen and even better a Progress Listener UI to give useful feedback.
Tips for Mobile: : Joshua Marinacci has listed some useful tips for mobile.
I'm sure there are more... 'll keep updating this list...