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

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

Expand Messages
  • Marcel Duran
    ... source: http://tech.groups.yahoo.com/group/jslint_com/message/598 [Non-text portions of this message have been removed]
    Message 1 of 8 , Nov 29, 2010
    • 0 Attachment
      >
      >
      > Can anyone provide a use case (one that's actually being used in production
      > code, and not just an academic exercise) in Javascript where using the array
      > literal syntax doesn't provide what you need?
      >
      > new Array(n + 1).join('*'); // string containing n asterisks
      source: http://tech.groups.yahoo.com/group/jslint_com/message/598


      [Non-text portions of this message have been removed]
    • abyssoft@ymail.com
      ... 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
      Message 2 of 8 , Nov 29, 2010
      • 0 Attachment
        --- In jslint_com@yahoogroups.com, Marcel Duran <contact@...> wrote:
        >
        > >
        > >
        > > Can anyone provide a use case (one that's actually being used in production
        > > code, and not just an academic exercise) in Javascript where using the array
        > > literal syntax doesn't provide what you need?
        > >
        > > new Array(n + 1).join('*'); // string containing n asterisks
        > source: http://tech.groups.yahoo.com/group/jslint_com/message/598
        >
        >
        > [Non-text portions of this message have been removed]
        >

        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.
      • Marcel Duran
        ... 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
        Message 3 of 8 , Nov 30, 2010
        • 0 Attachment
          >
          >
          > 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]
        • 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 4 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 5 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.