Message from discussion
Anonymous functions garbage collection
Received: by 10.66.76.130 with SMTP id k2mr665871paw.16.1347539645175;
Thu, 13 Sep 2012 05:34:05 -0700 (PDT)
X-BeenThere: nodejs@googlegroups.com
Received: by 10.68.195.39 with SMTP id ib7ls8465011pbc.9.gmail; Thu, 13 Sep
2012 05:33:54 -0700 (PDT)
Received: by 10.66.75.106 with SMTP id b10mr607341paw.32.1347539634814;
Thu, 13 Sep 2012 05:33:54 -0700 (PDT)
Received: by 10.66.75.106 with SMTP id b10mr607340paw.32.1347539634802;
Thu, 13 Sep 2012 05:33:54 -0700 (PDT)
Return-Path: <vnykm...@gmail.com>
Received: from mail-pz0-f47.google.com (mail-pz0-f47.google.com [209.85.210.47])
by gmr-mx.google.com with ESMTPS id vw9si5061697pbc.2.2012.09.13.05.33.54
(version=TLSv1/SSLv3 cipher=OTHER);
Thu, 13 Sep 2012 05:33:54 -0700 (PDT)
Received-SPF: pass (google.com: domain of vnykm...@gmail.com designates 209.85.210.47 as permitted sender) client-ip=209.85.210.47;
Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of vnykm...@gmail.com designates 209.85.210.47 as permitted sender) smtp.mail=vnykm...@gmail.com; dkim=pass header...@gmail.com
Received: by daks35 with SMTP id s35so1637593dak.34
for <nodejs@googlegroups.com>; Thu, 13 Sep 2012 05:33:54 -0700 (PDT)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
d=gmail.com; s=20120113;
h=message-id:date:from:user-agent:mime-version:to:subject:references
:in-reply-to:content-type;
bh=NQzBMaUsD1v1w0kopbi1NMNgK0ya1xjWSFhIzlyyRzE=;
b=Z7Rx9X+9cZmu1tY/LV2vlqhIcNaYPjAkigvgfcBM1/LIr395KbeioQdgVSmRb3m0rr
Sz5dXWq/UmI1cNrLAcBdyJLizNgzxIXf8Nzmo/wfY53T1Wol9M/MROqfZ0WP1q816eSD
h9byl3mLsazBnrMd5pHvn0e/CnLccIqxTxaNEL0/D3pbpWBjNowscrCA5i7XAZx3Mgnd
DXX+oUTTjA8CQCnEQlbDyQEpPgZfZB3bZaSSr+HH61bsubROmtjcgqv0J07VRDjoEsIx
vA9yja7MsOESrlvo1NBAYF3gNe5WbrE5ijXt9USJ7N6N9Xd/7HemBqFh37LrJXIgp4G1
zjsA==
Received: by 10.68.193.196 with SMTP id hq4mr4701972pbc.32.1347539634669;
Thu, 13 Sep 2012 05:33:54 -0700 (PDT)
Return-Path: <vnykm...@gmail.com>
Received: from [192.168.1.25] ([182.71.151.98])
by mx.google.com with ESMTPS id hx9sm13008178pbc.68.2012.09.13.05.33.52
(version=SSLv3 cipher=OTHER);
Thu, 13 Sep 2012 05:33:53 -0700 (PDT)
Message-ID: <5051D2AE.7090...@gmail.com>
Date: Thu, 13 Sep 2012 18:03:50 +0530
From: Vinayak Mishra <vnykm...@gmail.com>
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:15.0) Gecko/20120828 Thunderbird/15.0
MIME-Version: 1.0
To: nodejs@googlegroups.com
Subject: Re: [nodejs] Anonymous functions garbage collection
References: <a2190a65-fc4c-41a4-a95b-178c9280fafb@googlegroups.com>
In-Reply-To: <a2190a65-fc4c-41a4-a95b-178c9280fafb@googlegroups.com>
Content-Type: multipart/alternative;
boundary="------------070908010008030909000109"
This is a multi-part message in MIME format.
--------------070908010008030909000109
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
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 nodejs@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+unsubscribe@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
--
Regards,
Vinayak
--------------070908010008030909000109
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
I don't see any problem with your approach, and I would probably
have done the same. Yet, <tt>JSLint</tt> would remind about making
functions within loop. Well, should not matter unless you're looping
over arrays with <i>some</i> thousands elements.<br>
<br>
As an alternative, if code readability/simplicity is a concern, why
not go with <tt><a href="https://github.com/caolan/async">async</a></tt>?<br>
<br>
<tt>var execute = function (item, cb) {</tt><tt><br>
</tt><tt> </tt><tt>/* do something meaningful with item, fire cb
when done */</tt><tt><br>
</tt><tt> };</tt><tt><br>
</tt><tt><br>
</tt><tt>var arr = getInitialData();</tt><tt><br>
</tt><tt>async.forEach(arr, execute, function (err) {</tt><tt><br>
</tt><tt> // check for error and/or proceed further</tt><tt><br>
</tt><tt>});</tt><br>
<br>
You could use one of many available control flows, check out <a
href="https://github.com/caolan/async/blob/master/README.md">many
more control flows</a>. 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 <tt>async</tt> as it is well written
and <a href="https://npmjs.org/package/async">thoroughly tested</a>
piece of code.<br>
<br>
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.<br>
<br>
Please share your thoughts.<br>
<br>
<br>
<div class="moz-cite-prefix">On 09/13/2012 11:30 AM, Maxim Kazantsev
wrote:<br>
</div>
<blockquote
cite="mid:a2190a65-fc4c-41a4-a95b-178c9280fafb@googlegroups.com"
type="cite">It is a pretty typical approach to use an anonymous
function for asynchronous calls from inside a loop:
<div><br>
</div>
<div>
<blockquote class="gmail_quote" style="margin: 0px 0px 0px
0.8ex; border-left-width: 1px; border-left-color: rgb(204,
204, 204); border-left-style: solid; padding-left: 1ex; ">var
a = getInitialData();<br>
for (var i = 0, len = a.length; i < len; i++) {<br>
(function(el) {<br>
/* do something non-blocking here */<br>
})(a[i]);<br>
}</blockquote>
<div><br>
</div>
<div>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.</div>
</div>
<div><br>
</div>
<div>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?</div>
-- <br>
Job Board: <a moz-do-not-send="true"
href="http://jobs.nodejs.org/">http://jobs.nodejs.org/</a><br>
Posting guidelines: <a moz-do-not-send="true"
href="https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines">https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines</a><br>
You received this message because you are subscribed to the Google<br>
Groups "nodejs" group.<br>
To post to this group, send email to <a class="moz-txt-link-abbreviated" href="mailto:nodejs@googlegroups.com">nodejs@googlegroups.com</a><br>
To unsubscribe from this group, send email to<br>
<a class="moz-txt-link-abbreviated" href="mailto:nodejs+unsubscribe@googlegroups.com">nodejs+unsubscribe@googlegroups.com</a><br>
For more options, visit this group at<br>
<a moz-do-not-send="true"
href="http://groups.google.com/group/nodejs?hl=en?hl=en">http://groups.google.com/group/nodejs?hl=en?hl=en</a><br>
</blockquote>
<br>
<pre class="moz-signature" cols="72">--
Regards,
Vinayak</pre>
</body>
</html>
--------------070908010008030909000109--