Subcribe via RSS

Signals in AS3: simplicity and performance overview

July 15th, 2011 | No Comments | Posted in ActionScript3

For those who don’t know about Signals, it’s an open sorce project created by Robert Penner that give us a different approach when working with events in our projects. I have discovered it quite late but I wanted to try it and run some performance tests.
The simplicity and ease of use it’s the first thing we notice with Signals.
Let’s take a quick look at how we can use them.

A basic example comparison:

1
2
var dispatcher = new EventDispatcher();
dispatcher.dispatchEvent(new MyEvt(MyEvt.COMMAND));
1
2
var mySignal = new Signal();
signal.dispatch();

We see here that with Signals we don’t need an EventDispatcher or an event object. But now let’s see how to add the listeners:

1
2
3
4
5
dispatcher.addEventListener(MyEvt.COMMAND, onEventHandler);

function onEventHandler(e:Event):void {
// our handler code
}
1
2
3
4
5
mySignal.add(mySignalHandler);

function mySignalHandler():void {
// our handler code
}

If we need to send arguments with our Signal we can do it easily as in the following example:

1
2
3
4
5
6
7
var mySignal = new Signal(param1:String, param2:int);
mySignal.add(mySignalHandler);
signal.dispatch("test", 23);

function mySignalHandler(param1:String, param2:int):void {
// our handler code
}

Notice the parameters in the constructor. They are optional but it’s handy to use them as we will get an error in case the parameter types don’t match.

Signals provides a nice new method to register a listener but for a single use. As seen in the following example, we can listen for a Signal only once.

1
2
3
4
5
6
7
8
9
var mySignal = new Signal();
mySignal.addOnce(mySignalHandler);
signal.dispatch();
signal.dispatch();
signal.dispatch();

function mySignalHandler():void {
// our handler code
}

No matter how many Signals are dispatched, our listener will be called only for the first Signal. Nice one :)

Next we can see how we can remove listeners in the next comparison:

1
dispatcher.removeEventListener(MyEvt.COMMAND, onEventHandler);
1
mySignal.remove(mySignalHandler);

With Signals we can remove all the listeners at once:

1
mySignal.removeAll(mySignalHandler);

That will cover the basic Signals, but there is more advance things that we can do.

Imagine you want to receive the target of the Signal. In that case we have a new type of Signal called DeluxeSignal.

See below how it’s used:

1
2
3
4
5
6
7
8
9
var myDeluxeSignal = new DeluxeSignal(this);
myDeluxeSignal.add(myDeluxeSignalHandler);

myDeluxeSignal.dispatch(new GenericEvent());

function myDeluxeSignalHandler(e:GenericEvent): void {
// e.target
// e.signal
}

In the constructor we need to set the target and in the dispatch of the Signal we create a GenericEvent object that will be sent to the listener. Through it we will be able to retrive the target and the signal itself.

And finally, when we need to work with native events we find the last type of Signal called NativeSignal. This Signal needs an EventDispatcher in order to dispatch.

Following we can see a comparison for a mouse click event:

1
2
3
4
5
this.addEventListener(MouseEvent.CLICK, onClicked);

function onClicked(e:MouseEvent):void {
// our handler code
}
1
2
3
4
5
6
var clicked:NativeSignal = new NativeSignal(this, MouseEvent.CLICK, MouseEvent);
clicked.add(onClicked);

function onClicked(e:MouseEvent):void {
// our handler code
}

For this Signal we need to send to the constructor the target, the event type we want to broadcast and the event class. For listening this Signal is the same but we will receive the class we specified in the constructor.

As you see it’s simple and easy to work with it but now let’s take a look at the performance:

  • If we dispatch Events without listeners we get Signals are around 20-25% faster than Events.
  • If we dispatch DeluxeSignal instead (it’s closer to a regular event as we can get target and event) we notice Events are 60-65% faster than Signals.
  • If we dispatch events with listeners we get Events are around 10-15% faster than Signals and around 60% faster against DeluxeSignals.

This tests have been done with Flex 4.5.1 dispatching 1000000 events.

As far as I know Signals performed much better than Events, but it seems Events wins the battle so far.

It’s always good trying to find the balance and get the best performance to our projects. Sometimes it’s a combination of different approaches when we get the best results.

If you want to try Signals you can download the project here.


Happy coding!

Flash Player 11 and AIR 3 in public beta available

July 14th, 2011 | No Comments | Posted in Flash

Adobe Labs has just released the public beta of Flash Player 11 and AIR 3 for desktop. This is an exciting announcement as we can start playing with all the new features that are now available to our joy :)

PowerFlasher releases FDT 4.5

July 4th, 2011 | No Comments | Posted in General


FDT 4.5 has been released recently and it comes with interesting new features:

  • Latest 4.5 & 3.6 Flex SDK Support
  • Namespace Generation & Refactoring
  • iOS & Android Mobile Templates

You can get detailed information here including the list of improvements as well.

Great work PowerFlasher!