1

I wonder if there is any difference in execution speed between:

Object( e.target ).labelTextID

and

MovieClip( e.target ).labelTextID

e.target in that case is a MovieClip, but it doesn't matter.

MyFantasy512
  • 690
  • 1
  • 9
  • 20

3 Answers3

2

There aren't any difference. Take a look on this sample code:

        var obj:Object = new MovieClip();
        var v1:Object = Object(obj);
        var v2:MovieClip = MovieClip(obj);

Both type castings produce the same op codes:

        _as3_findpropstrict Object
        _as3_getlocal <1> 
        _as3_callproperty Object(param count:1)
        _as3_coerce Object
        _as3_setlocal <2> 

        _as3_findpropstrict flash.display::MovieClip
        _as3_getlocal <1> 
        _as3_callproperty flash.display::MovieClip(param count:1)
        _as3_coerce flash.display::MovieClip
        _as3_setlocal <3> 

The conclusion is also confirmed by benchmark:

    var obj:Object = new MovieClip();

    var v1:Object, v2:MovieClip, i:int;
    var t:uint = getTimer();

    for(i = 0; i < 1000000; i++)
        v1 = Object(obj);

    trace("Object: ", (getTimer() - t));

    t = getTimer();
    for(i = 0; i < 1000000; i++)
        v2 = MovieClip(obj);

    trace("MovieClip: ", (getTimer() - t));

output:

Object:  92
MovieClip:  90
fsbmain
  • 5,267
  • 2
  • 16
  • 23
  • while the cast itself takes the same amount of time, how you use it afterwards will affect your speed, i.e. it's generally quicker to access variables/methods through specific class instances rather than generic (`Object`) – divillysausages Sep 17 '13 at 21:13
1

In terms of what type of object you cast to, I cannot be sure, but in terms of how you are doing your casting I would take a look at this answer: https://stackoverflow.com/a/14268394/1346390

The TLDR of it is, use "as" for casting, since it's both faster and safer, since it throws a TypeError if it fails.

Community
  • 1
  • 1
Valentin
  • 1,731
  • 2
  • 19
  • 29
0

There must be no difference between this cast. In both cases the compiler is instructed to explicit cast the e.target no matter of type of e.target.

Azzy Elvul
  • 1,403
  • 1
  • 12
  • 22