Don’t use Number to iterate over for-loops

Jacob Wright
March 18th, 2008

A while back I read a couple of blog posts about the slowness of using uint and int to iterate through for-loops. I needed to do some testing for a little project today and found this is false.

When using the “i” variable in for(var i = 0; i < length; i++) as an input into mathematical operation, especially when doing fractions, Number is faster for obvious reasons. This was established in the posts of the previously mentioned blogs. But when simply iterating over an array which is a very common use-case for for-loops uint is faster. Here is my test setup:

var value:Object;
var arr:Array = new Array(1000000);
var length:uint = arr.length;
var startTime:Number = getTimer();

for (var i:uint = 0; i < length; i++)
value = arr[i];

var endTime:Number = getTimer();
trace(”Total Time:”, endTime - startTime);

I was getting around 210 for uints and around 230 for Number. Not a big difference, but I feel dumb for always using Number for this sort of thing without even thinking about how it works.

Using Your Own Custom Metadata in AS3

Jacob Wright
March 15th, 2008

Flex 3 gives us a great new feature, custom metadata tags. Now, I know you could actually use custom metadata in Flex 2, but you would have to add "-keep-as3-metadata MyTag" to every single project that utilized these custom tags. In Flex 3, if you add "-keep-as3-metadata MyTag" to a library (using compc to compile a SWC or a Flex Builder Library project), then EVERY project that uses that SWC will automatically keep the "MyTag" metadata tags. This allows custom libraries that utilize these tags for development.

Would be cool to create a library to hook up listeners so you can create listeners like this:

[Listen(obj="this.closeButton", event="click")]
public function closeClickHandler(event:MouseEvent) {...}

You'd use -keep-as3-metadata Listen in the libraries compiler options. Maybe if you want I could post a tutorial on doing something like this. Drop me note and let me know if there is interest.

Flight Stealth, a Flex Component Alternative

Jacob Wright
February 7th, 2008

The first question you probably have after seeing the title of this post is "why would you make an alternative to Flex?" Or perhaps, "how could you even think of competing with Flex?" Honestly, we aren't trying to compete. This will be open source. And alternatives are always nice to have. We could talk about how difficult it can be to extend the Flex components, how limited the styling is, or how Flex makes complicated things easy and easy things complicated, but the fact of the matter is we just thought building an alternative would be fun.

Now, that being said, I think we're going to have a KILLER alternative to Flex. We (Tyler Wright, my twin, and I) have been wanting to do something like this for awhile. After hearing Ely Greenfield's Flex roadmap presentation at MAX we got all excited about it again. Much of what was presented there are things we've had in mind and wanting to build. I've been working on a proof-of-concept over the last few weeks, although we've worked on it off and on for a couple of years, and we are definitely headed in the right direction. I'll list here a bunch of the stuff we've got worked out so far.

(more...)

Binding to Container children in Flex

Jacob Wright
December 29th, 2007

I guess this week is my week for creating AS3/Flex hacks. I created an XML encoder/decoder that will accept a class-map and create your model from your XML and visa-versa. As part of that I have display objects as part of the model that needs saving out. I suppose you could liken it to MXML.

In order for the XML encoder to work with DisplayObjectContainers I had to give it a way to work with an ArrayCollection to add or remove the children. So I subclassed LayoutContainer and gave it a property called "children" which is an ArrayCollection. Then I set up as a listener to it and every time something is added or removed I use the corresponding DisplayObjectContainer methods to update the display. Pretty slick! The only downside is that someone could add anything to the children ArrayCollection and break things since I am expecting DisplayObjects only. I could add error checking and make it fix itself when that happens... maybe another time.

Here's my listener which responds to the "collectionChange" event of children:

protected function onChildrenChange(event:CollectionEvent):void
{
    var i:Number;
    var items:Array = event.items;
    var location:Number = event.location;
    var oldLocation:Number = event.oldLocation;
    var numItems:Number = items.length;
    var child:DisplayObject;
    var propChange:PropertyChangeEvent;
   
    switch (event.kind)
    {
        case CollectionEventKind.ADD:
            for (i = 0; i <numItems; i++)
            {
                child = items[i] as DisplayObject;
                if (child)
                    addChildAt(child, location + i);
            }
            break;
       
        case CollectionEventKind.REMOVE:
            for (i = 0; i <numItems; i++)
            {
                child = items[i];
                if (child)
                    removeChild(child);
            }
            break;
       
        case CollectionEventKind.MOVE:
            setChildIndex(getChildAt(oldLocation), location);
            break;
       
        case CollectionEventKind.REPLACE:
            for (i = 0; i <numItems; i++)
            {
                propChange = items[i] as PropertyChangeEvent;
                var oldChild:DisplayObject = propChange.oldValue as DisplayObject;
                var newChild:DisplayObject = propChange.newValue as DisplayObject;
                if (oldChild && newChild)
                {
                    var index:Number = getChildIndex(oldChild);
                    removeChildAt(index);
                    addChildAt(newChild, index);
                }
            }
            break;
       
        case CollectionEventKind.REFRESH:
            removeAllChildren();
            for each (child in _children)
                addChild(child);
            break;
       
        case CollectionEventKind.RESET:
            removeAllChildren();
            for each (child in _children)
                addChild(child);
            break;
       
        case CollectionEventKind.UPDATE:
            // don't need to do anything
            break;
    }
}

Automatic Casting Using XML

Jacob Wright
December 28th, 2007

I am loving the E4X stuff in ActionScript 3. Once you get your mind wrapped around it and if you don't have to deal with namespaces it is very nice. Namespaces can be useful too, I just wish you could turn a namespace-agnostic-mode on and off.

I just discovered a cool trick I thought I'd share with you. I found it when I was iterating through the properties of two objects to test if they were equal. The properties of one object were all Strings because it was parsed from a comma-delimited file. So, some Booleans or Numbers were strings of "true" or "10.5" while their counter-parts I was testing against were the real true and 10.5. You'd think to accomplish this you'd need a bunch of if statements that cast the values to the same type and test them. That could get messy. And what about casting Booleans. Has to be manual since there is no parseBoolean method. And NULL, same thing.

So, I tried using E4X XML to test it. Doing this:

trace("true" == true, XML("true") == true);

Gives you this:
false true

This is because E4X will do automatic casting for you. Wonderful! Now I had my solution. Just cast all values to XML first before comparing it (or assigning it). Works with Numbers (of all types), Booleans, and Strings.

AIR Active Record

Jacob Wright
December 19th, 2007

I've been working with AIR quite a bit since my side project is in AIR. I thought it would be pretty cool to create an active record implementation in AIR since I've got one on the server side.

I have to use synchronous database connections for it so that everytime I access an object's related properties I don't have to use a callback, though it could be refactored to do that. Not my idea of fun though, and after an excellent presentation by Jason Williams, "Working with Persistent Data in AIR," where he showed how fast it was to retrieve data from an AIR database I figured it would be just fine.

Currently I don't have metadata allowing you to define how every property maps to a field in the database, nor do I have many "special case" hooks for this and that. I figured that any AIR app you write will be using a fresh database and not a legacy system. How many desktop apps using a SQLite database will you be updating with AIR and using the same database file? Maybe I'm wrong, but I don't think so.

I also added a piece that you can run which will create and update database tables based off the ActiveRecord object's properties. So you have a nice update mechanism for the database when you update your app using the updater api.

I'll be open-sourcing the code when I have time (isn't that always the case), but you can get the code now, test it out, and let me know what enhancements you'd like to see. Sorry, no documentation currently. If I like the ideas or they're requested enough I'll be sure to add them in. :)

Oh, and when you unzip it you'll see "flight" is the package Tyler (my twin) and I are using for our projects. It started when he and Rob Taylor started a component set in AS2 under the name but AS3 and Flex 2 came out and they never finished. Maybe an AS3 set will happen someday.

Update: AIR Active Record is open sourced.

Next Page »