Flex Data Binding Performance Pitfall

A friend of mine recently asked me to help him troubleshoot some performance problems with his Flex application. In his scenario he had a large list of data and wanted to filter the data such that each time the search string grew by a character the complex filter would only be run on the results of the previous filter. A very simple approach to this is just to keep a record in each item indicating if the item matched the filter for each search string. Even though the filter function will still run for each item, the complex part of the filter function could easily be isolated and only run for the subset of data which matched the previous filter. This may not be the best way to do this (I’m open to other suggestions) but it was simple. You can see the results of my first attempt here:

Demo 1 (Enter a few numbers in the search field and see how many items are being filtered and how long it takes.)
Source 1

Notice that in order to keep track of how many items are reaching the main part of the filter I increment a Bindable variable inside the filter function:

[Bindable] private var ffCount:Number = 0;
...
private function ff(item:Object):Boolean
{
...
    ffCount++;
...
}

Now since I’ve been programming in Flex for over four years you’d think I’d know better. Every time a Bindable object is modified Flex does a bunch of event dispatching and other plumbing. In this case the Label which is bound to the ffCount variable is also doing a bunch of work each time ffCount changes. As you can see, the results are not great. It takes almost a full second (on my machine) to do the filter on 20,000 items. Obviously this isn’t right.

Lesson learned: A Flex application should never, ever, ever, update Bindable objects in filter functions or other places where the user won’t ever be able to distinguish something changing that rapidly.

A better approach is to update a non-Bindable variable in the loop or filter function, then when the loop or filter is done, copy the non-Bindable variable to a Bindable one. You can see the updated demo using this approach here:

Demo 2
Source 2

This approach is about 10x faster! And only a little bit more work. In this case it’s very easy to be notified when the filter has completed and then update the Bindable variable:

if (event.kind == CollectionEventKind.REFRESH)
{
    ffCount = _ffCount;
}

In retrospect this seems like a silly mistake but I would not be surprised if this sort of thing is causing performance problems in many Flex applications. Data Binding is a very powerful part of Flex but it can also cause performance problems when used incorrectly. I hope this helps some of you to avoid this pitfall. Let me know what you think.

Video: Flex Effects and Skinning

Intuit has been using Flex in some very exciting ways recently. One of those is to use Flex as a developer SDK for QuickBase. This allows developers to easily build a great user interface on top of the solid and in-the-cloud back-end of QuickBase. You can find out more about this in the QuickBase Developer Program.

A few weeks ago I presented to developers in the QuickBase Developer Program about making Flex applications look and feel great by adding effects and by skinning components. Intuit has allowed me to repost that presentation here. Let me know what you think.

Very First Steps in Flex - Now Available!

Bruce Eckel and I have been working on a new Flex book aimed at those totally new to Flex. It will be very short (about 140 pages) and provide just the right information to get someone started with Flex. The book will be called “First Steps in Flex” and should be available soon.

If you would like to be notified when “First Steps in Flex” is available we’ve setup a Google Group for book related announcements.

For those who just can’t wait to check it out… A preview of the book is now available! We’ve taken a few chapters from “First Steps in Flex” and created a DZone Refcard called “Very First Steps in Flex”. Please go “Vote Up” the announcement on DZone.com. Then go download the Refcard and let us know what you think.

The Flex Show Episode 50: Interview with James Ward and Bruce Eckel

Listen to the Flex Show interview with Bruce Eckel and me. Then let me know what you think.

Atlanta JUG and Chicago Flex and AIR Developers Group

In a few weeks I’ll be stopping by the Atlanta Java User Group and the Chicago Flex and AIR Developers Group:

Atlanta - August 19, 2008 - Rich Internet Applications with Flex and Java

Chicago - August 20, 2008 - The Future of Flex, Flash Player, and AIR

Hope to see you there!

Video: Flex Best Practices

Here is a recording from my presentation about Flex Best Practices / Flex Architectural Patterns at the NLJUG. Sorry that the audio is not great on this one. It turned into more of a discussion than a presentation. Let me know what you think.

Video: Flex and Java

Here is the recording of a presentation I gave at the NLJUG a few weeks ago on Flex and Java. I hope you find this useful. Let me know what you think.

Upcoming Conference Presentations: GeoWeb, OSCON, and LinuxWorld

Over the next few weeks I’ll be presenting at a few conferences:

GeoWeb - Map Based RIA Development Using Adobe Flex and AIR with Duane Nickull

OSCON - Flex: the Open Source SDK for RIAs also with Duane

LinuxWorld - Building Rich Internet Applications with an Open Source Stack

I hope to see some of you at those conferences.

The RIA Puzzle: Shaping the Future of the Internet with Adobe Platform Technologies

Recently I did an interview with Anthony Franco, President of EffectiveUI, about Flex, Adobe AIR, and Rich Internet Applications. Check it out and let me know what you think.