AS3: Performance question calling an event function with null param
Lately I needed to call a listener function without an actual listener like so:
foo(null);
private function foo(event:Event):void
{
//do something
}
So I was wondering if there is a significant difference regarding performance between this and using the following, in which I can prevent the null in calling the function without the listener, but am still able to call it with a listener as well:
foo();
private function foo(event:Event = null):void
{
}
I am not sure wether it is just a question of style, or actually bad practice and I should write two similar functions, one with and one without the event param (which seems cumbersome to me).
开发者_运维问答Looking forward to your opinions, thx.
As bitc stated, there is no difference. Please note, this is only the case in strictly typed calls, where the compiler can determine the default arguments at compiletime and actually include the default value as parameter.
When calls are untyped, the player has to look up default arguments at runtime, which does cost some speed, but probably shouldn't be too much, compared with all runtime type checks/casts that need to be carried out in such a case.
However, this is not such a good practice. You should seperate it into two functions. One, which recieves the event and extracts necessary data from it, and then passes it to another function, that will actually process it. The actual logic within that function becomes more reusable. also refactoring becomes easier, since you can simply move the function (as in a pull up) without breaking the code. Last but not least, it clearly separates responsibility.
Of course, if whatever the actual response to the event is, neither processes an event, nor is too complex, than splitting is a little exagerated.
greetz
back2dos
These two compile to the exact same bytecode. I have checked with a binary diff.
精彩评论