Dynamic Flash

Confessions of a serial code abuser
  • rss
  • Home
  • MTASC
  • Archives
  • About me
  • Goodies
    • Base64 encoder/decoder class
  • My Bookshelf
  • My Talks

Efficiently populating a DataGrid control

Sunday, 29 January 2006

At Featurecreep HQ we're currently working on the next iteration of our flagship content management system. Whilst thrashing out the UI for a particular part of the administrative interface, we came across a requirement to display a lot - and I mean a lot - of information in a DataGrid control. Having had previous bad experience with the Tree control, I wanted to do a proof of concept before commiting to the idea.

My first attempt was just as you'd expect: create a loop that, on each iteraction, added a row to the DataGrid using the addItem() method...

PLAIN TEXT
Actionscript:
  1. for (var i:Number = 0; i <100000; i++) {
  2.     dg.addItem({test:"Test"});
  3. }

This worked out to be painfully slow in my PowerMac G5 at work, taking over 45 seconds to populate the DataGrid. Ouch! Not only is this an unacceptable amount of time to force the user to wait, it also meant that the 15 second script timeout alert popped up at least 3 times. Faced with such an uphill struggle, it was at this point that I started to think about adding the rows in batches over a number of seconds using intervals. Fortunately I don't give up that easily, and set about finding some way to optimise the code.

I wondered if it might be quicker if I built the array of items first and then added them to the DataGrid using the dataProvider property. I knew that the Array class has some mix-in methods applied when you include any V2 component in your SWF, and I figured that these must be optimed for speed. The method I was particularly interested in was the addItem() method so I decided to give it a go...

PLAIN TEXT
Actionscript:
  1. var dp:Array = new Array();
  2. for (var i:Number = 0; i <100000; i++) {
  3.   dp.addItem({test:"Test"});
  4. }
  5. dg.dataProvider = dp;

The total time for populating the DataGrid this time was around 19 seconds; a 3x increase in speed compared to adding each item directly to the DataGrid. Not bad, but still under the default 15 second script timeout limit and so still not acceptable.

On further testing it seemed that it was taking only a fraction of a second to assign my data provider to the DataGrid. The rest of the execution time was taken up by populating the data provider in the loop. Thinking that the mix-in methods maybe weren't so optimised after all, I tried using Array.push() to see if that was any quicker...

PLAIN TEXT
Actionscript:
  1. var dp:Array = new Array();
  2. for (var i:Number = 0; i <100000; i++) {
  3.   dp.push({test:"Test"});
  4. }
  5. dg.dataProvider = dp;

Result! Total execution time for the above code was just over 4 seconds. That's almost 5x the speed of using the mix-in methods, and 15x the speed of using DataGrid.addItem(). I'm not sure what extra functionality the mix=in methods perform, but for our purposes they certainly aren't worth a 5x increase in execution time.

So, the moral of this little tale is not to use the data provider mix-in methods unless speed of execution isn't a requirement or you absolutely must; this probably applies to all of the list-based controls in the V2 UI component set.

My next task is to see if I can find a more efficient way of sorting the data when you click on a column header. Wish me luck!

PS. Please excuse the small code text and lack of syntax highlighting. I've just updated to Wordpress 2.0 but my usual syntax highlighting plugin has not yet been updated.

Categories
General
Comments rss
Comments rss
Trackback
Trackback

« How not to develop ANY software, let alone Open Source Sorry about the blog barf »

5 responses

I experienced the same frustration using a combination of xml

Craig Jones | Friday, 31 March 2006 | 6:27 pm

I experienced the same frustration using a combination of xml connector, dataset and datagrid.

This method works very well. I trimmed approximately 90% of the processing time off populating our datagrid.

By using a combination of the split command to parse a modified Name, Value string into an array and then pushing the resulting array into a second array prior to assigning the dataset.dataprovider works fantastic.

Can we see this flagship CMS?

Mark | Thursday, 06 April 2006 | 10:52 pm

Can we see this flagship CMS?

Mark: We're not currently licensing this to third parties to

Steve | Friday, 07 April 2006 | 7:34 am

Mark: We’re not currently licensing this to third parties to develop with, though of course that is something we plant to do eventually. You can find out more about out CMS here.

thanks for that man, you saved me a lot of

chris | Friday, 09 June 2006 | 6:51 am

thanks for that man, you saved me a lot of trouble on this project. I have three combobox’s inside the datagrid (clients wish) even though it doesnt load under 4 secs like ur it does the job under 10. cheers!

in AS3 you need dg.dataProvider = new DataProvider(dp); to works

john doe | Friday, 28 September 2007 | 12:54 pm

in AS3 you need

dg.dataProvider = new DataProvider(dp);

to works

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

About Dynamic Flash

Steve Webster is a Senior Web Developer for Yahoo! in London, UK.

He is more than a little concerned that he defines himself in terms of his career, and that he talks about himself in the third person.

Find out more

Recent Posts

  • Yahoo! London seeks Junior Developers
  • I am Singularity
  • Yahoo! Astra component library 1.1 released
  • On X-UA-Compatible
  • foundationas3.com has launched

Tags

ActionScript actionscript 3.0 air book conference designer filereference file upload Flash flex internet internet explorer jobs junior microsoft opportunities singularity08 skin urlrequest web developer web development Web Standards Yahoo!

Stuff

Singularity?
Flex.org - The Directory for Flex
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox