Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #16200
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!weretis.net!feeder4.news.weretis.net!news.teledata-fn.de!newsfeed.arcor.de!newsspool2.arcor-online.net!news.arcor.de.POSTED!not-for-mail |
|---|---|
| Content-Type | text/plain; charset="UTF-8" |
| Message-ID | <3738235.beI7O1F77D@PointedEars.de> (permalink) |
| From | Thomas 'PointedEars' Lahn <PointedEars@web.de> |
| Reply-To | Thomas 'PointedEars' Lahn <cljs@PointedEars.de> |
| Organization | PointedEars Software (PES) |
| Date | Thu, 27 Sep 2012 20:50:15 +0200 |
| User-Agent | KNode/4.4.11 |
| Content-Transfer-Encoding | 8Bit |
| X-Face | %i>XG-yXR'\"2P/C_aO%~;2o~?g0pPKmbOw^=NT`tprDEf++D.m7"}HW6.#=U:?2GGctkL,f89@H46O$ASoW&?s}.k+&.<b';Md8`dH6iqhT)6C^.Px|[=M@7=Ik[_w<%n1Up"LPQNu2m8|L!/3iby{-]A+#YE}Kl{Cw$\U!kD%K}\2jz"QQP6Uqr],./"?;=4v |
| Face | iVBORw0KGgoAAAANSUhEUgAAADAAAAAwBAMAAAClLOS0AAAAGFBMVEXTxa4RFk5dUWANED8PFEfy7+MGBiW+n3ZNF/QuAAACaElEQVQ4jVXUwVOcMBQG8Dc7Rc4PUntdWV2uxjDpGaGeozOp1woar4jd5t/v9wLstMwsA/ntlxdCAgUc1hjTc9/JCZfGoo3wG3HdmdAWrIJRHe7GM/TmpY5VFefuVcAkkPbLIaN8rmPmjloyZxgyR3GuJ4K0AGtJ2htz8o7yqikm759fldQXaMpbDzjKAG+8v+AugVTOPO5DOjLvGtUYQwh0CPjnVMyGd+8/GfUB5nLKJDD2aLDh5HYyMDJGDwQIo2ZmZcKbowNmAdB/AzyFhrmF2MHRb0QJJfaAnwGB6orZhoykLzJtGwF/xpYxI1dswomiUj3gTuAIqCn/4C7cULwGNBtwMTk3Y4LfKB5YUaOKBKYtpplm7u0vip8tU1NWWyI/7XdcSuIDoMt6rVHMWT0DbjHPGqDqZVSa6zleLcUTcIKLoMv3ueJluALtAo9B302zPPlrtiVScRdCjXvVh3e3JpYa/jjkuC9N+LrBMlz/eAN4eQijX2EdLo6c5tGGHwLyHFtXk89dDGHwCVhG9T0S/j55AhRZgkMCmUQXJ49TnS1wnQDvw0eAh9ICeMmEFbCnPMFzjAvsWoEWEFdYEx+S0MoUZ1gT1wId8+AF3Bl2OoEu906AUHx5VLw/gXYg/x84loOah/2UYNrgiwSwGO7RfUzVBbx/kgpckumGOi6QirtD6gkLTitbnxNol47S2jVc2vsN5kPqaAHT8uUdAJM4v/DanjYOwmUjWznGfwB7sGtAtor5BgofDuzaRj4kSQAqDakTsKORa3Q3xKi3gE1fhl71KRMqrdZ2AWNNg/YOhQyrVBnb+i+nEg4bsDA+egAAAABJRU5ErkJggg== |
| Subject | Re: silly question on closure |
| Newsgroups | comp.lang.javascript |
| References | <a9704993-d83a-4392-8a65-82755e9ba320@u15g2000yql.googlegroups.com> <k3kocg$8ok$1@news.albasani.net> <7227299.QoKgMUOf8L@PointedEars.de> <k40fod$k2d$1@news.albasani.net> |
| Followup-To | comp.lang.javascript |
| MIME-Version | 1.0 |
| Lines | 62 |
| NNTP-Posting-Date | 27 Sep 2012 20:50:15 CEST |
| NNTP-Posting-Host | ac0bd732.newsspool3.arcor-online.net |
| X-Trace | DXC=?Ta<Y=T=@:b016@cHD@m;jMcF=Q^Z^V3h4Fo<]lROoRa8kF<OcfhCOko;k`8g]<bOgDZm8W4\YJNlR=i2=[N6Y6jCG]WYDmLC;aG@nRK`B6a^l |
| X-Complaints-To | usenet-abuse@arcor.de |
| Xref | csiph.com comp.lang.javascript:16200 |
Followups directed to: comp.lang.javascript
Show key headers only | View raw
Stefan Weiss wrote:
> On 2012-09-24 01:10, Thomas 'PointedEars' Lahn wrote:
>> Stefan Weiss wrote:
>>> On 2012-09-22 14:04, justaguy wrote:
>>>> The closure takes so many lines while the function has only 2 lines to
>>>> get the same thing done. So, the question is, why do we use closure?
>>>
>>> You don't, just like you don't actually use "Hello, world" in production
>>> code. This was obviously just an example for how closures work. If this
>>> was code intended for actual use, your "just a function" variant would
>>> be better suited (provided it only returned the value, like in the
>>> "closure" example).
>>
>> You are mistaken as you are overlooking that creating that Array instance
>> and populating it with elements requires resources – memory and runtime.
>> Since we have only automated garbage collection, and the function may be
>> called a number of times, it is better to use a closure here, so that the
>> object would only be created, and the array only populated, once.
>
> I would say that depends on the use case.
Of course it does.
> I have no doubt that closures can offer performance benefits even with a
> simple 4-element array like this, but unless you're running the function a
> million times in a tight loop, the difference will be hardly noticable.
Of course. You are missing the point.
> There's a often a tradeoff between legibility and performance.
> Introducing temporary variables, helper functions, layers of
> indirection, etc, can all hurt performance, but we still do it because
> it helps us keep our code clean, understandable, and maintainable.
False dichotomy.
> In the vast majority of cases, JS performance is not a problem
> (anymore).
You are very much mistaken.
> In this particular case, the goal was "understanding closures". It's a
> fair assumption to make that the code was not running in a tight loop, and
> that the OP was not overly troubled by the performance of his script.
I have disputed your assertion that closures are to be avoided with a two-
line function because they would not be worth it in any case.
> You're writing a general purpose library, the OP is learning JS. Your
> goals are different. Recommending the closure style whenever possible,
> regardless of circumstances, smacks of premature optimization.
Which is why I am not doing that.
PointedEars
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
silly question on closure justaguy <lichunshen84@gmail.com> - 2012-09-22 05:04 -0700
Re: silly question on closure Stefan Weiss <krewecherl@gmail.com> - 2012-09-22 18:18 +0200
Re: silly question on closure justaguy <lichunshen84@gmail.com> - 2012-09-22 18:41 -0700
Re: silly question on closure Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-24 01:10 +0200
Closures (was: silly question on closure) Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-24 13:14 +0200
Re: silly question on closure Stefan Weiss <krewecherl@gmail.com> - 2012-09-27 05:04 +0200
Re: silly question on closure Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-27 20:50 +0200
Re: silly question on closure Stefan Weiss <krewecherl@gmail.com> - 2012-09-27 21:29 +0200
Re: silly question on closure "Tom de Neef" <tdeneef@qolor.nl> - 2012-09-22 20:00 +0200
Re: silly question on closure justaguy <lichunshen84@gmail.com> - 2012-09-22 18:42 -0700
Re: silly question on closure justaguy <lichunshen84@gmail.com> - 2012-09-23 07:31 -0700
Re: silly question on closure Stefan Weiss <krewecherl@gmail.com> - 2012-09-23 18:49 +0200
Re: silly question on closure justaguy <lichunshen84@gmail.com> - 2012-09-23 13:33 -0700
Re: silly question on closure justaguy <lichunshen84@gmail.com> - 2012-09-23 13:38 -0700
Re: silly question on closure Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-24 20:49 +0200
Re: silly question on closure Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2012-09-24 01:14 +0200
Re: silly question on closure dann90038@gmail.com - 2012-09-23 15:37 -0700
csiph-web