Wednesday, May 2, 2007

SWF Size: ActionScript 3 versus ActionScript

I was concerned I wasn’t going to get compact SWF files from ActionScript 3 (AS3) like I was getting from ActionScript 2 (AS2). But after running some simple tests, I am now thinking AS3 will not be a problem.

Unfortunately, I’m still disappointed with the large SWF size from MXML projects. However, I still plan to use Flex 2 with MXML to develop my back-end tools to take advantage of its rapid development qualities. At the same time I will pick up some practical experience with MXML. Maybe after seeing MXML in action, I might become less resistant to using it in my Rich Internet Applications (RIA).

SWF files are compressed and this can cause confusion when it comes to analyzing and comparing size. For example, when I refactor my AS2 source code to be more efficient, I sometimes find it results in a larger size. Simply changing the order of things in your code can affect the size because of the compression. Fortunately, I found Flash 8 AS2 SWF files end up amazingly small regardless of my coding style.

My tests are crude and leave plenty of room for misinterpretation and argument. Nonetheless, I have concluded AS3 files grow incrementally at about the same pace even though they start out initially about 500 bytes larger.

I wrote test code in AS2 and AS3 to build 2 TextFields. I compared the resulting SWF file size by compiling 3 different versions of each. The first, everything was commented out. The second, 1 TestField was created. Finally the third, 2 TextFields were created.

AS2
var mvTextField1:TextField = createTextField
(
  "T1",
 getNextHighestDepth(),
 0,
 0,
 200,
 20
);
mvTextField1.text = "T1";

var mvTextField2:TextField = createTextField
(
  "T2",
 getNextHighestDepth(),
 0,
 20,
 200,
 20
);
mvTextField2.text = "T2";

AS3
package
{
 import flash.display.Sprite;
 import flash.text.*;

 public class ActionScriptSizeTest extends Sprite
 {
  public function ActionScriptSizeTest()
  {
   var lvTextField1:TextField = new TextField();
   lvTextField1.x = 0;
   lvTextField1.y = 0;
   lvTextField1.width = 200;
   lvTextField1.height = 20;
   lvTextField1.text = "T1"
   addChild( lvTextField1 );

   var lvTextField2:TextField = new TextField();
   lvTextField2.x = 0;
   lvTextField2.y = 20;
   lvTextField2.width = 200;
   lvTextField2.height = 20;
   lvTextField2.text = "T2"
   addChild( lvTextField2 );

  } // Constructor

 } // public class

} // package

AS2 / AS3 SWF Size Comparison, bytes
Test AS2 AS3 Increase

No code

36

566

530

1 TextField

144

667

523

2 TextFields

172

694

522



The results actually show AS3 to be better incrementally than AS2 but the differences are too small to be significant.  Remember, the compression confuses everything.  I was worried that bringing in a TextField might require some extra library code.  It turns out adding a TextField adds only a little more than 100 bytes for both ActionScripts.

The whole coding strategy changes when you migrate to ActionScript 3, so I can’t say if the equivalent program would be smaller or larger.  I’m never going to write the same application again.  At the very least I would write it with more features.  So I’ll never know.

No comments: