dcsimg
A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: nested functions, functions as variables, and performance

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    2

    nested functions, functions as variables, and performance

    I'm writing a pretty extensive Flash app, and am wondering how best to declare all the functions needed, and best practices relating to it.

    So far I have been creating functions for everything on the main code line as in example 1 below - no nested functions or functions as variables.

    Is it better to create some functions inside others for performance reasons? In particular, event listener functions. Or is it even better still to create the handler functions as variables so they are cleaned up after the function executes?

    A simple example if the above is confusing:
    1)
    Code:
    function test() {
    ...
    loader.addEventListener(Event.COMPLETE, loaderHandler);
    ...
    }
    function loaderHandler(e:Event) {...}
    2)
    Code:
    function test() {
    ...
    loader.addEventListener(Event.COMPLETE, loaderHandler);
    ...
    function loaderHandler(e:Event) {...}
    }
    3)
    Code:
    function test() {
    ...
    var loaderHandler:Function = function(e:Event) {...}
    loader.addEventListener(Event.COMPLETE, loaderHandler);
    ...
    }

    Thanks in advance.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    I don't know about performance (I might expect that class level methods might be handled differently from locally scoped functions), but for the sake of correctness I usually recommend the first way.

    If you do declare your functions inside another function, then each time you execute the outer function, you will get a new inner function. That would definitely have an impact on performance. But your examples were loaders, which are morel likely to happen only once.

    It seems like you have a handle on the scoping issues inherent to locally defined functions, so I recommend just doing some benchmarks. Please post your results, too.

  3. #3
    Total Universe Mod jAQUAN's Avatar
    Join Date
    Jul 2000
    Location
    Honolulu
    Posts
    2,429
    I can't say for sure either but a call stack is a call stack no matter where the functions originate. The processor has just as much work to do.

    The real impact is on memory. Flash's garbage collector technically only dumps stuff that nobody has a reference to. Event listeners can be particularly nasty in this light. Flash manages a listener list for each object which for the most part sticks around even if the object is nullified. This means if you don't explicitly remove the listener from the loader object before you delete the loader, the listener will continue to listen in the background. addEventListener has a useWeakReference param but is more of a fail safe than a guarantee.

    If you declare listeners locally, 1) it may be gone by the time the asynchronous action complets and 2) you may not be able to target and remove it when the time comes.

    I kinda doubt samples 2 and 3 will work at all.

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