I don't see any problem with your approach, and I would probably have done the same. Yet, JSLint would remind about making functions within loop. Well, should not matter unless you're looping over arrays with /some/ thousands elements.

As an alternative, if code readability/simplicity is a concern, why not go with async <https://github.com/caolan/async>?

var execute = function (item, cb) {
/* do something meaningful with item, fire cb when done */
  };

var arr = getInitialData();
async.forEach(arr, execute, function (err) {
  // check for error and/or proceed further
});

You could use one of many available control flows, check out many more control flows <https://github.com/caolan/async/blob/master/README.md>. The advantage is the package provides different control flows without having to keep re-inventing them. Say, you want to perform certain activity in parallel, or in series, or based on some conditionals, while we could write code to achieve that, I would recommend using async as it is well written and thoroughly tested <https://npmjs.org/package/async> piece of code.

Where you want to place the execute code depends on many factors, like do we need to access/modify other variables in scope, if independent operations, they can go in library functions.

Please share your thoughts.


On 09/13/2012 11:30 AM, Maxim Kazantsev wrote:
It is a pretty typical approach to use an anonymous function for asynchronous calls from inside a loop:

    var a = getInitialData();
    for (var i = 0, len = a.length; i < len; i++) {
      (function(el) {
        /* do something non-blocking here */
      })(a[i]);
    }


JSLint doesn't like this code with "Don't make functions within a loop" warning, and it is actually right since it really creates a new anonymous function on every single loop iteration. An obvious solution is to declare this function outside a loop, but it would make a code less readable. Even if a declaration would just precede the loop: you see a call here, you see a declaration somewhere else, and here you are, lost all your attention.

My question is how bad this approach is for an overall performance? In particular, how fast and efficient a garbage collection of anonymous functions is? How much memory a typical anonymous function can consume and how long it may exist in a memory?
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

--
Regards,
Vinayak

--
Job Board: http://jobs.nodejs.org/
Posting guidelines: 
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

Reply via email to