A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Assign function to variable (for speed)

  1. #1
    Developer
    Join Date
    Apr 2007
    Location
    UK
    Posts
    324

    Assign function to variable (for speed)

    hi Flash gurus!

    I've read in various places that it is faster to assign a function to a variable

    e.g var mround:Function = Math.round()

    But I dont understand how that can be faster. Doesn't it still have to look up Math.round() when mround is called or does it copy the code from Math.round() into 'mround'?

    I've accepted that it's faster - I just want to know why.

  2. #2
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    Your syntax is slightly out, should be

    var mround:Function = Math.round;

    It's quicker because mround is a pointer to the method, it doesn't have the overhead of having to find the Math class, and then finding the round method within that class, it points to it directly.

    So it doesn't copy code, it's just a short cut ( Via a pointer ). It's the same way as this is quicker

    var mc:MovieClip=_root.child1.child2.child3;
    mc._x=100;

    over,

    _root.child1.child2.child3._x=100;

    Squize.

  3. #3
    Developer
    Join Date
    Apr 2007
    Location
    UK
    Posts
    324
    Cool. Thanks for the explaination

  4. #4
    Zombie Coder EvilKris's Avatar
    Join Date
    Jun 2006
    Location
    Fukuoka, Japan.
    Posts
    796
    Hmm.didn't know that. Cheers Squize

  5. #5
    Hype over content... Squize's Avatar
    Join Date
    Apr 2001
    Location
    Lost forever in a happy crowd...
    Posts
    5,926
    np guys. If either of you can face trawling through a fair bit of spam, there are lots of handy little tips like this in the optimisation thread ( via the knowledge base sticky ).

    Squize

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center