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

Re: new Array(n)

Expand Messages
  • 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 1 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 2 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 3 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.