Don’t use Number to iterate over for-loops

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

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.

Why is WordPress so easy to install, so painful to update?

I don’t think there is a better open-source blog engine on the market than WordPress, but why is it so painful to update it? You have to back up the database, download the files, copy over the config file, themes, uploads, and any other stuff you need to the new installation, and then rename the folders quickly so the new one fills the space of the old one.

Why can’t we just get the files that have changed downloaded and the database updated by pressing a big blue “Update” button?

How to Roll Back Changes using Subversion

A question I’ve often gotten is “How do I roll back some changes I made in Subversion?” I’m the company expert in Subversion here at mediaRAIN (another one of my hats) so I thought I’d answer the question one more time.

Subversion is great as it allows you to track every change you’ve made and go back to a previous version if needed. Sometimes you only need to go back to an earlier revision temporarily (for example to tag the project at that point). But other times you (or that lousy co-worker who left on vacation yesterday!) have introduced bugs or committed something you shouldn’t have. Now you need to roll back the broken revision(s). Looking at TortoiseSVN’s menu or going through the list of command-line options you’ll see … gasp! … subversion forgot a roll-back feature! How could they do that?! Isn’t that one of the main reasons for using a versioning system like Subversion?


How to listen to Flash events that don’t bubble

I learned a little trick the other week and thought I’d share. Sometimes you might need to listen to a non-bubbling event “up the chain” somewhere, but of course, since it is not bubbling, that can be hard. The trick is that event events that don’t have bubbling still have the capture phase, so if you flip that capture flag to true in the event listener then you can now listen to any events dispatched by any child views whether they are bubbling or not. Example:

 // listen for some event (this one is even custom) that does not bubble // make sure to set true on capture stage.addEventListener("customNonBubbling", myListener, true); ...   <mx:Button id="myButton" click="dispatchEvent(new Event('customNonBubbling'))"/> 

Note: this only works on display objects as far as I know. Capture and bubbling don’t apply to objects that are not on the display list.

Hacking the Flex SDK

The Flex SDK is finally fully open-source. So I thought I’d have a little fun and implement my own Binding classes to be used when using the [Bindable] metadata tag in an ActionScript 3 project.

The first thing I did of course was to checkout the Flex SDK from subversion. They’ve got instructions on doing all that in their wiki. The wiki also has some great instructions for downloading and installing ANT and the other things you’ll need to build the SDK. Look further down on the previously linked to page in the section “Building and testing.”

Once I had the project checked out I downloaded ANT per the Mac-specific instructions on the wiki. I the ran `source` and then `ant -q main` just like they said and voila! I had built my own SDK. Of course, it was basically the same as the one already installed with Flex Builder.

I then started digging into the code and found in the sdk/modules/compiler/src/java/flex2/compiler/as3/binding/BindableProperty.vm template file that is used for the auto-generated binding code. I took out some of the imports, changed the [Bindable(event=”propertyChange”)] on line 62 to [Bindable(event=”${entry.propertyName}Change”)], and changed every place it had dispatchEvent(PropertyCha…. to dispatchEvent(new Event(“${entry.propertyName}Change”). This will allow my binding class that just looks for {propertyName}”Change” event for binding instead of using the expensive describeType function.

Next, I compiled the whole SDK and had issues, because the Flex framework needs to use the old binding. SO DON’T DO THAT. After backtracking and fixing things up I changed to the sdk/modules/compiler/ directory and THEN ran `ant -q main`. Now my mxmlc will use the new binding template but the old framework SWCs weren’t compiled wrong with it.

To check out the new generated classes compile your project using `mxmlc -keep`. That creates a folder called “generated” that has all those classes including your new bindable changes in it. Now in Flex Builder, you can add a your new SDK and compile your own projects. Haven’t had time to actually test this out yet and I’ve gotta run, but let me know if you get it working.