Loading ...
Sorry, an error occurred while loading the content.

Re: [jslint] Re: new Array(3)

Expand Messages
  • Morgaut Alexandre Louis Marc
    Very interesting test :-) thanx jsperf ;-) but there was some errors... To be comparable, in those scripts str = a.join( char ); and str = a.join( * ); should
    Message 1 of 8 , Nov 30, 2010
    • 0 Attachment
      Very interesting test :-)

      thanx jsperf ;-)

      but there was some errors...
      To be comparable, in those scripts
      str = a.join('char');
      and
      str = a.join('*');
      should be
      str = a.join(char);



      Please note also that priority must not be always focused on perf

      These must also be considered:
      - Readability of the code
      - Less code === Less potential error hidden in the pattern (wrong copy/paste or error while typing)

      So I may be more confident to see these powerful patterns only where they are required
      (maybe even with a comment to improve the readability)

      About these two readable solutions, the use of "new Array(n)" vs "a.length = n"
      I still prefer the first one...

      here:

      var tmpArray, myString;

      tmpArray = [];
      tmpArray.length = 20;
      myString = tmpArray.join('*');

      -> The initialisation of my string variable require to use a temporary variable which will pollute my scope when debugging

      there

      var myString;

      myString = [];
      myString.length = 20;
      myString = myString('*');

      -> myString is first an array (arghhh) before becoming the desired string... and still multiple lines for simple initialisation

      finally

      var myString = new Array(10).join('*');

      -> The initialisation is on one line only and directly provide the good type


      On Nov 30, 2010, at 11:56 AM, Marcel Duran wrote:

      > >
      > >
      > > http://tech.groups.yahoo.com/group/jslint_com/message/614
      > >
      > > Provides an elegant & effective solution without the use of new Array(n+1)
      > > and it operates quickly in all modern browsers.
      > >
      > > I've just made a JSPerf benchmark comparing these various ways of creating
      > a string of n repeated character:
      >
      > http://jsperf.com/string-of-n-character
      >
      > Seems like new Array and [].length are the slowest ones at least in latest
      > version of Chrome. Feel free to run this test in different browsers and let
      > browserscope take care of benchmark results.
      >
      > Marcel
      >
      > [Non-text portions of this message have been removed]
      >
      >



      [Non-text portions of this message have been removed]
    • Douglas Crockford
      ... Readability is paramount. So the inherent off-by-one problem in the trick makes it hazardous. Certainly it does not justify keeping new Array in the safe
      Message 2 of 8 , Nov 30, 2010
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, Morgaut Alexandre Louis Marc <morgaut@...> wrote:
        > Please note also that priority must not be always focused on perf
        >
        > These must also be considered:
        > - Readability of the code
        > - Less code === Less potential error hidden in the pattern (wrong copy/paste or error while typing)

        Readability is paramount. So the inherent off-by-one problem in the trick makes it hazardous. Certainly it does not justify keeping new Array in the safe subset. The fact that new Array(3) is not the same as [3] is a more serious problem than the need to make a string with the wrong number of stars in it.

        Is there another essential use for new Array?
      Your message has been successfully submitted and would be delivered to recipients shortly.