Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.javascript > #17935

Re: Two versions of code - advantages and differences

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!eternal-september.org!feeder.eternal-september.org!aioe.org!.POSTED!not-for-mail
From Cezary Tomczyk <cezary.tomczyk@gmail.com>
Newsgroups comp.lang.javascript
Subject Re: Two versions of code - advantages and differences
Date Fri, 04 Jan 2013 09:15:02 +0100
Organization Aioe.org NNTP Server
Lines 90
Message-ID <kc6320$ngm$1@speranza.aioe.org> (permalink)
References <kc4uud$fam$1@speranza.aioe.org> <kc54ui$hvr$1@news.albasani.net>
NNTP-Posting-Host uvfIanNdPkadtm415cehVA.user.speranza.aioe.org
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding 7bit
X-Complaints-To abuse@aioe.org
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/17.0 Thunderbird/17.0
X-Notice Filtered by postfilter v. 0.8.2
Xref csiph.com comp.lang.javascript:17935

Show key headers only | View raw


W dniu 2013-01-04 00:41, Stefan Weiss pisze:
> On 2013-01-03 22:58, Cezary Tomczyk wrote:
>> Version 1
>>
>> var el = document.getElementById('test');
>> var fn = function(){
>>       if( !el ){
>>           // fallback if el is not available and then return
>>       }
>>       return el;
>> };
>>
>> Version 2
>>
>> var fn = (function(){
>>       var el = document.getElementById('test');
>> 	
>>       if(el){
>>           return function(){
>> 	    return el;
>>       	}
>>       } else {
>>           // fallback if el is not available and then return
>>       }
>> }());
>>
>> Correct me, if I am wrong.
>>
>> a) Version 1 has an advantage over Version 2, because Version 2 is
>> slower (?) and consumes more memory (?).
>> b) Version 2 has a closure and inside fn everything is not available
>> outside (except what return "return").
>
> Your interpretation is broadly correct, but the example is too trivial
> to make a good distinction (especially concerning performance or
> memory). If that's all there is to it, version 1 would usually be
> preferred for its simplicity.

As I mentioned the examples is just a simple examples. Yes, they are 
trivial, but just wanted to show general idea. They can contains more 
complicated code.

> There are several situations where a closure like version 2 would be
> better suited. For example:
>
> - When the function performs something computationally expensive (or
> taking a long time), the result will be "remembered" and available for
> subsequent calls to 'fn'.

But same can be done in Version 1.

> - When, depending on the availability of 'el', two different functions
> will be returned.

That's true, but this can be done also in Version 1. Functions can be 
defined and returned.

> - When it uses an internal state (for example, a counter).

This also can be done in Version 1 and I do not see advantage here over 
Version 2.

> - When it uses private helper functions that are useless in the
> surrounding scope.

Yes. This is the advantage Version 2 over Version 1. Of course, in some 
cases and it depend on what is required.

> In general, if the same goal can be achieved in a simple and a
> complicated way, choose simple :)

I agree :-) Just wanted to get some confirmations if I am thinking in 
the right way ;-)

> By the way, one way of keeping it simple would be to use
>    function fn () {}
> instead of
>    var fn = function () {};
> for version 1.
>
> Some people recommend to always use function expressions, but IMO, if
> you know how variable and function declarations are handled when an
> execution context is entered, this is unnecessary in most cases.

You mean this?
http://stackoverflow.com/a/3344397

-- 
Cezary Tomczyk
http://www.ctomczyk.pl/

Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-03 22:58 +0100
  Re: Two versions of code - advantages and differences Stefan Weiss <krewecherl@gmail.com> - 2013-01-04 00:41 +0100
    Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-04 09:15 +0100
      Re: Two versions of code - advantages and differences Gregor Kofler <usenet@gregorkofler.com> - 2013-01-04 11:26 +0100
        Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-04 14:29 +0100
          Re: Two versions of code - advantages and differences Gregor Kofler <usenet@gregorkofler.com> - 2013-01-04 14:56 +0100
            Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-04 16:52 +0100
    Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-04 09:19 +0100
  Re: Two versions of code - advantages and differences Gregor Kofler <usenet@gregorkofler.com> - 2013-01-04 11:25 +0100
  Re: Two versions of code - advantages and differences Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-04 15:31 +0100
    Re: Two versions of code - advantages and differences David Mark <dmark.cinsoft@gmail.com> - 2013-01-05 08:37 -0800
      Re: Two versions of code - advantages and differences Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-05 18:14 +0100
        Re: Two versions of code - advantages and differences David Mark <dmark.cinsoft@gmail.com> - 2013-01-05 10:51 -0800
          Re: Two versions of code - advantages and differences Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-05 20:46 +0100
            Re: Two versions of code - advantages and differences David Mark <dmark.cinsoft@gmail.com> - 2013-01-05 17:12 -0800
              Re: Two versions of code - advantages and differences David Mark <dmark.cinsoft@gmail.com> - 2013-01-06 17:43 -0800
              Re: Two versions of code - advantages and differences Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-07 03:58 +0100
                Re: Two versions of code - advantages and differences David Mark <dmark.cinsoft@gmail.com> - 2013-01-06 19:23 -0800
                Re: Two versions of code - advantages and differences Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-07 05:19 +0100
    Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-06 22:30 +0100
      Re: Two versions of code - advantages and differences Stefan Weiss <krewecherl@gmail.com> - 2013-01-07 02:35 +0100
        Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-07 22:45 +0100
      Re: Two versions of code - advantages and differences Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-07 04:36 +0100
        Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-07 23:06 +0100
          Re: Two versions of code - advantages and differences Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-01-08 12:35 +0100
            Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-09 08:34 +0100
  Re: Two versions of code - advantages and differences David Mark <dmark.cinsoft@gmail.com> - 2013-01-05 09:13 -0800
    Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-06 11:09 +0100
      Re: Two versions of code - advantages and differences David Mark <dmark.cinsoft@gmail.com> - 2013-01-06 12:56 -0800
        Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-07 22:53 +0100
  Re: Two versions of code - advantages and differences Luc Yen <luc@goal.tw> - 2013-01-05 12:54 -0800
    Re: Two versions of code - advantages and differences Cezary Tomczyk <cezary.tomczyk@gmail.com> - 2013-01-06 21:38 +0100

csiph-web