JavaFX - Bind with Caution!


JavaFX "bind" and "on replace" are really nice and useful features. But recently I came across many misuse of this feature which results in performance issues. Here we will look into couple of common usage which leads to performance issues..

An example application is shown below...

<script src="http://dl.javafx.com/1.2/dtfx.js"></script> <script src="/rakeshmenonp/resource/BindCaution/BindCaution.js"></script>

For Applet mode, click on above image

For standalone mode

Example: In below code, the user wants to perform "update" operation when ever there is any change in x, y, w and height. The initial values of these variables are set to 0. Later as and when the application is shown on screen, actual values of these are updated.

var x = on replace update()}
var y = on replace update()}
var width = on replace update()}
var height = on replace update()}

function update() {
    /\*\*
     \* Performs complex update process!
     \*/
}

But during this process, the "update" method is invoked 8 times! 4 times when the variable is initialized to 0 and another 4 times when the actual value is set. Instead if we perform a simple check to ensure that all variables are set before update, we could have easily avoided performing the complex operation so many times and improve performance.

function update() {
    if((x > 0and (y > 0and (width > 0and (height > 0)) {
        /\*\*
         \* Performs complex update process!
         \*/
    }
}

Another common usage is binding to value of Slider or ScrollBar. Objective is to perform some operation as and when the value is updated by user.

var slider = Slider {
    min: 0
    max: 100
    value: 50
}

var sliderValue = bind slider.value on replace {
    /\*\*
     \* Perform Complex Operation!!!!
     \*/
}

But this will end up performing the operation many many times before the actual value required by user is set. Also the slider won't be responsive, since we are constantly repeating the task. So instead of performing operation for each and every value with in the interval, we may just wait for few milli-seconds before performing the actual operation.

var slider = Slider {
    min: 0
    max: 100
    value: 50
}

var timeline = Timeline {
    keyFrames: KeyFrame {
        time: 50ms
        action: function() {
            /\*\*
             \* Perform Complex Operation!!!!
             \*/
        }
    }
}

var sliderValue = bind slider.value on replace {
    timeline.playFromStart();
}

In above code a Timeline is started when ever the user changes the slider value. The actual operation is performed only after few (50ms in above code) milli-seconds. This will ensure that we won't perform complex operation until user settles for a value.

Also bind is used for layout of nodes along with boundsInLocal, boundsInParent etc. Instead try to use various layouts such as Flow, HBox, Panel, Stack, Tile, VBox and LayoutInfo.

Also refer to Girish's blog which talks about another common performance issue related to dynamic update of nodes.

These are not the only issues or only solutions, the objective is to just caution the usage, so that you can optimize the code as per your requirement. Try it out and let me know feedback

Source:

<script type="text/javascript">var dzone_url = "http://blogs.sun.com/rakeshmenonp/entry/javafx_bind_with_caution";</script> <script type="text/javascript">var dzone_style = '2';</script> <script language="javascript" src="http://widgets.dzone.com/widgets/zoneit.js"></script>

Comments:

Informative, thanks !

Posted by Vaibhav on July 07, 2009 at 01:47 PM IST #

Good post, and a reminder to not be so lazy with your code.

Thanks

Posted by ChrisW on July 09, 2009 at 06:04 PM IST #

@Chris Thanks!

Posted by Rakesh Menon on July 10, 2009 at 08:02 AM IST #

Thanks for your usage tips.

Posted by Charles Tam on September 01, 2009 at 09:43 PM IST #

Post a Comment:
Comments are closed for this entry.
About


The views expressed on this blog are my own and do not necessarily reflect that of my organization

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today