Wednesday, January 7, 2009

HTTPService performance of the various result formats

Today I was reading about the various result formats options available for using with HTTPService requests. These formats are:
  • object (default) - the resulting xml is returned as an ObjectProxy, or an ArrayCollection of ObjectProxy objects
  • array - the root object is returned as the first item in an Array
  • xml - returns the well formed xml (as an XMLNode object)
  • e4x - returns the response as an XML object
  • flashvars - result is formatted into name=value pairs like "name=value&on=true"
  • text - returns the response as a String object
Have you ever wondered which one is the fastest?

I ran some tests on an XML file that had 22,000 lines, and here are the results:
  • object: 610ms
  • array: 600ms
  • xml: 244ms
  • flashvars: 121ms
  • text: 51ms
  • e4x: 113ms
And here is the code I used to calculate the times:
private var startTime:int;

private function loadXMLFile(format:String):void {
    var service:HTTPService = new HTTPService();
    service.url = "huge.xml";  // located in the project src directory
    service.resultFormat = format;
    service.headers["Pragma""no-cache";  // no caching of the file
    service.headers["Cache-Control""no-cache";
    service.addEventListener(ResultEvent.RESULT, resultHandler);
    startTime = getTimer();
    var token:AsyncToken = service.send();
    token.resultFormat = format;
}
   
private function resultHandler(event:ResultEvent):void {
    var time:int = getTimer() - startTime;
    trace(event.token.resultFormat + ": " + time + "ms");
}

Notes: there are static constants for the 6 result formats in HTTPService.
I haven't done any calculations on how long it would take to parse the XML vs. working with the ObjectProxy objects. And obviously if you are working with other files types besides XML then the results will be different.

Out of curiosity I also tested how long it would take to load the same file using a different method using the URLLoader and URLRequest classes. The result was usually around 80ms, but that was just to return the contents of the file. When converted into an XML object it was closer to 120ms.

6 comments:

Martin said...

Cool to know :) Thanks for your post.

Martin said...

Starting with a 1.5 s performance on loading a big XML as object (default format), I could improve perf to 0.15 s by using e4x format.

Again, thanks for the post.

Chris Callendar said...

I noticed that too, you definitely don't want to load an XML file using the default object format unless you really have to. Using the e4x (or the xml) format will be significantly faster. Thanks for the comment.

Unknown said...
This comment has been removed by a blog administrator.
Yordan Yanakiev said...

Cool !

Thank you Chris !

Emilio said...

Thank you, here i found how to add custom headers in my FLEX app.