cost of calling a class for a static const in as3
I'm wondering about the effectiveness, cost of, or resources used to call a public static const from a class
Let's, hypothetically, say I have a class that has quite a few resources and calling the constructor is about 40kb of开发者_运维问答 memory.
Is there any difference in adding static constants to the same class as opposed to creating a small class with just the constants?
In the case of an event dispatcher, the class listinging to the event would have something like this addEventListener(myClass.Holla,onHolla);
or addEventListener(myClassEventNames.Holla,onHolla);
Is there a difference (significant enough) to warrant using an extra class to store the event names?
No. There will be no significant difference.
Accessing a static member has nothing to do with the constructor; so you won't have any performance issues.
If the event in question is a custom event, the convention is to declare the string constants for all events of a particular class of event (subclass of flash.events.Event
) in that event subclass itself. For example, all mouse event constants are declared in MouseEvent
, all menu related events are defined in MenuEvent
etc.
This convention will help you in code-completion if you're using the Flex mxmlc compiler. Let's say you have added the following metadata tag on top of a class definition (MyClass).
[Event(name="randomEvent", type="com.domain.events.CustomEvent")]
public class MyClass extends EventDispatcher { }
Now you declare an instance of this class and type in addEventListener:
var myClass:MyClass = new MyClass();
myclass.addEventListener(
You'll get CustomEvent.RANDOM_EVENT
in the autocomplete dropdown list. Without the metadata tag, it will just give you the two default items (activate and deactivate). The metadata tag tells the compiler that this class dispatches an event of class CustomEvent
and type randomEvent
- and the compiler assumes that the string constant is defined as per convention and gives you CustomEvent.RANDOM_EVENT as the option.
Auto-completion might still work if you declare string constant in SomeOtherClass
and provide the name of that class in the metadata tag - but that would be misleading as the class in question does not dispatch any event of SomeOtherClass
You should profile this. If you set -compiler.debug=false -compiler.optimize=true, if you use SWFInvestigator you can see the opcodes necessary to read something.
This code:
var str:String;
str = ThisClass.TEST;
str = TEST;
str = SomeOtherClass.TEST;
str = CONFIGCONSTANT::TEST; // fastest
// str = ThisClass.TEST;
8 getproperty private::TEST //nameIndex = 30
// str = TEST;
12 getlex private::TEST //nameIndex = 30
// SomeOtherClass.TEST;
16 getlex pkg::SomeOtherClass //nameIndex = 29
18 getproperty TEST //nameIndex = 36
// CONFIGCONSTANT::TEST;
22 pushstring "test"
Either way, the speed decrease is minimal. You would have to do tens of millions of these operations to see any noticable decrease. However, you will save some processing, so you're going green! :)
精彩评论