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

Re: new Array(n)

Expand Messages
  • Douglas Crockford
    ... Suppose you need to construct a string containing n asterisks. It seems the fastest way to do that is new Array(n + 1).join( * )
    Message 1 of 11 , Jun 3, 2009
    View Source
    • 0 Attachment
      --- In jslint_com@yahoogroups.com, Randy Cox <rcox@...> wrote:
      >
      > On Tue, Jun 2, 2009 at 4:10 PM, Douglas Crockford <douglas@...> wrote:
      > > But then someone showed be a case where new Array(n) is actually useful.
      >
      > Will you share this useful case, or must it remain a mystery?

      Suppose you need to construct a string containing n asterisks.
      It seems the fastest way to do that is

      new Array(n + 1).join('*')
    • sandyhead25
      By faster do you mean faster for the interpreter to execute or faster for the person can save characters by not using a loop? If you mean anything but the
      Message 2 of 11 , Jun 4, 2009
      View Source
      • 0 Attachment
        By faster do you mean faster for the interpreter to execute or faster for the person can save characters by not using a loop?

        If you mean anything but the first condition I would say you should still ban the array constructor, because those couple extra characters a person must type is pale in comparison to the potential harm introduced by using a method prone to fault. The whole point of JSLint is to solve that problem literally by mandating style not prone to fault.
      • pauanyu
        ... For reference, here is the way to do this with the array literal: var foo = []; foo.length = n + 1; foo = foo.join( * );
        Message 3 of 11 , Jun 4, 2009
        View Source
        • 0 Attachment
          --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@...> wrote:
          >
          > Suppose you need to construct a string containing n asterisks.
          > It seems the fastest way to do that is
          >
          > new Array(n + 1).join('*')
          >

          For reference, here is the way to do this with the array literal:

          var foo = [];
          foo.length = n + 1;
          foo = foo.join('*');
        • benxwhite
          Here are the different bits of code that I tested. //Method 1 var cnt = 1000000, char = * , str = new Array(cnt + 1).join(char); //Method 2 var cnt = 1000000,
          Message 4 of 11 , Jun 4, 2009
          View Source
          • 0 Attachment
            Here are the different bits of code that I tested.

            //Method 1
            var cnt = 1000000,
            char = '*',
            str = new Array(cnt + 1).join(char);


            //Method 2
            var cnt = 1000000,
            char = '*',
            a = [],
            x, str;
            for (x = 0; x < cnt; x += 1) {
            a[x] = char;
            }
            str = a.join('');


            //Method 3
            var cnt = 1000000,
            char = '*',
            a = [],
            x, str;
            for (x = 0; x <= cnt; x += 1) {
            a[x] = '';
            }
            str = a.join('char');


            Looking purely at execution performance, it's really a mixed bag...
            In Firefox & Opera the Method 1 approach is faster.
            However in IE, Safari & Chrome Methods 2 & 3 were faster.

            Looking at this from a coding standpoint, I would say that Method 1 is a most elegant bit of code.



            --- In jslint_com@yahoogroups.com, "sandyhead25" <austin.cheney@...> wrote:
            >
            > By faster do you mean faster for the interpreter to execute or faster for the person can save characters by not using a loop?
            >
            > If you mean anything but the first condition I would say you should still ban the array constructor, because those couple extra characters a person must type is pale in comparison to the potential harm introduced by using a method prone to fault. The whole point of JSLint is to solve that problem literally by mandating style not prone to fault.
            >
          • Merlin
            ... Nice. String.prototype.replicate = function (n) { var a = []; a.length = n + 1; return a.join(this); }; var s = * .replicate(10); print(s); var c = cat ;
            Message 5 of 11 , Jun 4, 2009
            View Source
            • 0 Attachment
              --- In jslint_com@yahoogroups.com, "pauanyu" <pcxunlimited@...> wrote:
              >
              > --- In jslint_com@yahoogroups.com, "Douglas Crockford" <douglas@> wrote:
              > >
              > > Suppose you need to construct a string containing n asterisks.
              > > It seems the fastest way to do that is
              > >
              > > new Array(n + 1).join('*')
              > >
              >
              > For reference, here is the way to do this with the array literal:
              >
              > var foo = [];
              > foo.length = n + 1;
              > foo = foo.join('*');
              >

              Nice.

              String.prototype.replicate = function (n) {
              var a = [];
              a.length = n + 1;
              return a.join(this);
              };
              var s = '*'.replicate(10);
              print(s);
              var c = "cat";
              var t = c.replicate(2);
              print(t);
            • pauanyu
              ... I d be interested in seeing how those compare to the array literal: var foo = []; foo.length = n + 1; foo = foo.join( * ); In particular, how it compares
              Message 6 of 11 , Jun 5, 2009
              View Source
              • 0 Attachment
                --- In jslint_com@yahoogroups.com, "benxwhite" <ben.a.white@...> wrote:
                >
                > Here are the different bits of code that I tested.
                >
                > //Method 1
                > var cnt = 1000000,
                > char = '*',
                > str = new Array(cnt + 1).join(char);
                >
                >
                > //Method 2
                > var cnt = 1000000,
                > char = '*',
                > a = [],
                > x, str;
                > for (x = 0; x < cnt; x += 1) {
                > a[x] = char;
                > }
                > str = a.join('');
                >
                >
                > //Method 3
                > var cnt = 1000000,
                > char = '*',
                > a = [],
                > x, str;
                > for (x = 0; x <= cnt; x += 1) {
                > a[x] = '';
                > }
                > str = a.join('char');
                >
                >
                > Looking purely at execution performance, it's really a mixed bag...
                > In Firefox & Opera the Method 1 approach is faster.
                > However in IE, Safari & Chrome Methods 2 & 3 were faster.
                >
                > Looking at this from a coding standpoint, I would say that Method 1 is a most elegant bit of code.
                >

                I'd be interested in seeing how those compare to the array literal:

                var foo = [];
                foo.length = n + 1;
                foo = foo.join('*');

                In particular, how it compares to Method 1.
              • benxwhite
                ... //Method 4 var foo = []; foo.length = n + 1; foo = foo.join( * ); Method 4 compared very similar to Method 1 in speed, and actually slightly faster.
                Message 7 of 11 , Jun 6, 2009
                View Source
                • 0 Attachment
                  --- In jslint_com@yahoogroups.com, "pauanyu" <pcxunlimited@...> wrote:
                  >
                  > --- In jslint_com@yahoogroups.com, "benxwhite" <ben.a.white@> wrote:
                  > >
                  > > Here are the different bits of code that I tested.
                  > >
                  > > //Method 1
                  > > var cnt = 1000000,
                  > > char = '*',
                  > > str = new Array(cnt + 1).join(char);
                  > >
                  > >
                  > > //Method 2
                  > > var cnt = 1000000,
                  > > char = '*',
                  > > a = [],
                  > > x, str;
                  > > for (x = 0; x < cnt; x += 1) {
                  > > a[x] = char;
                  > > }
                  > > str = a.join('');
                  > >
                  > >
                  > > //Method 3
                  > > var cnt = 1000000,
                  > > char = '*',
                  > > a = [],
                  > > x, str;
                  > > for (x = 0; x <= cnt; x += 1) {
                  > > a[x] = '';
                  > > }
                  > > str = a.join('char');
                  > >
                  > >
                  > > Looking purely at execution performance, it's really a mixed bag...
                  > > In Firefox & Opera the Method 1 approach is faster.
                  > > However in IE, Safari & Chrome Methods 2 & 3 were faster.
                  > >
                  > > Looking at this from a coding standpoint, I would say that Method 1 is a most elegant bit of code.
                  > >
                  >
                  > I'd be interested in seeing how those compare to the array literal:
                  >
                  > var foo = [];
                  > foo.length = n + 1;
                  > foo = foo.join('*');
                  >
                  > In particular, how it compares to Method 1.
                  >

                  //Method 4
                  var foo = [];
                  foo.length = n + 1;
                  foo = foo.join('*');

                  Method 4 compared very similar to Method 1 in speed, and actually slightly faster. (approximately 10-40% in my tests)
                  It was not the fastest in every browser, but was the most consistent in performance across browsers.

                  I would suggest Method 4 for best performance, and Method 1 for cleanest.
                • pauanyu
                  ... Very interesting. I find it unintuitive how using arrays to work with strings can actually be faster than working directly with strings... yet here we are.
                  Message 8 of 11 , Jun 6, 2009
                  View Source
                  • 0 Attachment
                    --- In jslint_com@yahoogroups.com, "benxwhite" <ben.a.white@...> wrote:
                    >
                    > //Method 4
                    > var foo = [];
                    > foo.length = n + 1;
                    > foo = foo.join('*');
                    >
                    > Method 4 compared very similar to Method 1 in speed, and actually slightly faster. (approximately 10-40% in my tests)
                    > It was not the fastest in every browser, but was the most consistent in performance across browsers.
                    >
                    > I would suggest Method 4 for best performance, and Method 1 for cleanest.
                    >

                    Very interesting. I find it unintuitive how using arrays to work with strings can actually be faster than working directly with strings... yet here we are.

                    40% faster, you say? That's impressive, given how both appear to do exactly the same thing. It probably has to do with the fact that "new Array" does different things depending on what you pass to it.
                  Your message has been successfully submitted and would be delivered to recipients shortly.