Panzercrisis
Panzercrisis

Reputation: 4750

In AS3, is it more efficient to consolidate different event types into just one event type with multiple values?

Is one of these particularly more effiicient than the other?


1:

Within Class A:

var b:B = new B();
b.addEventListener(B.TYPE1, onType1);
b.addEventListener(B.TYPE2, onType2);
b.addEventListener(B.TYPE3, onType3);

Within Class B:

dispatchEvent(new Event(TYPE1));
.
.
.
dispatchEvent(new Event(TYPE2));
.
.
.
dispatchEvent(new Event(TYPE3));

2:

Within Class A:

var b:B = new B();
b.addEventListener(CEvent.TYPE, onCEvent);
.
.
.
private function onCEvent(pEvent:CEvent):void
{
    switch (pEvent.code)
    {
        case B.TYPE1:
            onType1();
            break;

        case B.TYPE2:
            onType2();
            break;

        case B.TYPE3:
            onType3();
    }
}

Within Class B:

dispatchEvent(new CEvent(TYPE1));
.
.
.
dispatchEvent(new CEvent(TYPE2));
.
.
.
dispatchEvent(new CEvent(TYPE3));

Class CEvent:

public class CEvent extends Event
{
    public static const TYPE:String = "cEvent";

    private var m_strCode:String;
    public function get code():String
    {
        return m_strCode;
    }

    public function CEvent(pCode:String, bubbles:Boolean=false,
            cancelable:Boolean=false)
    {
        super(TYPE, bubbles, cancelable);
        m_strCode = pCode;
    }

    override public function clone():Event
    {
        return new CEvent(m_strCode, bubbles, cancelable);
    }
}

My understanding has been that #2 will be a lot more time-efficient than #1, and right now, I'm needing to really work on finding various ways to make my code run almost as fast possible (almost as in the context of video game programming). Earlier on I wasn't using the second technique though, so there's gonna be a lot of work going into consolidating the different types strewn across my code. I need to ask first and make sure that this is halfway reasonable. Thanks!


EDIT

Something I came to realize a while after posting this was that ActionScript 3's event model is criticized for inefficiency largely over the fact that a brand new object is created each time an event is created. Creating objects can be a slightly bulky process, in and of itself, so an event model that is based on creating brand new objects over and over again is liable to be fairly inefficient as a direct result of that.

At the time I wrote this question, I wasn't aware this was the source of cricism, or of the fact that object creation was slow, and putting it in that light, neither one of the approaches above would really do much to get around that issue. (Approach 2 would probably be ultimately worse.) I don't want to put that down as an answer, but I do want that bit of context to be put down on this thread.

Upvotes: 1

Views: 147

Answers (1)

user797257
user797257

Reputation:

  1. Are you sure you need to optimize it?
  2. The difference will be likely negligible whichever way.
  3. Your switch is a half-baked ad hoc implementation of what EventDispatcher is meant to do. You are trying to invent a bicycle, and it is likely to have square wheels. This is the job of EventDispatcher to provide an instrument of adding or removing handlers, managing what handlers get called and when etc. If you are dissatisfied with that how EventDispatcher works (why?) - the way to address it is to either extend and change something within it, or write an alternative implementation. Trying to "fix" it locally for some particular case of a handful of functions will bring disorder into your code, making it less maintainable and, as it stands now, more verbose.

Upvotes: 2

Related Questions