Array element assignment

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Berislav Lopac

    Array element assignment

    Does anyone has experience/information about the performance of the array
    elements assignment?

    Specifically, which is more efficient:

    var myArray = new Array(a, b, c, d);

    or

    var myArray = new Array();
    myArray[] = a;
    myArray[] = b;
    myArray[] = c;
    myArray[] = d;


    Specifically, I'm talking about multi-dimensional arrays with, all together,
    several thousand elements in them.

    Berislav


  • Dietmar Meier

    #2
    Re: Array element assignment

    Berislav Lopac wrote:
    [color=blue]
    > Specifically, which is more efficient:
    >
    > var myArray = new Array(a, b, c, d);
    >
    > or
    >
    > var myArray = new Array();
    > myArray[] = a;[/color]

    This results into a syntax error. Do you mean:
    myArray[myArray.length] = a;
    ?

    ciao, dhgm

    Comment

    • Berislav Lopac

      #3
      Re: Array element assignment

      Dietmar Meier wrote:[color=blue]
      > Berislav Lopac wrote:
      >[color=green]
      >> Specifically, which is more efficient:
      >>
      >> var myArray = new Array(a, b, c, d);
      >>
      >> or
      >>
      >> var myArray = new Array();
      >> myArray[] = a;[/color]
      >
      > This results into a syntax error. Do you mean:
      > myArray[myArray.length] = a;
      > ?
      >
      > ciao, dhgm[/color]

      Of course. I was still thinking in PHP when writing this.

      Berislav


      Comment

      • Mick White

        #4
        Re: Array element assignment

        Berislav Lopac wrote:
        [color=blue]
        > Does anyone has experience/information about the performance of the array
        > elements assignment?
        >
        > Specifically, which is more efficient:
        >
        > var myArray = new Array(a, b, c, d);
        >
        > or[/color]
        Literals:
        var myArray = [a, b, c, d];
        //Not more efficient, perhaps, but less to write.
        //And:

        var my2dimArray = [[a,b,c,d],[1,2,3,4],["a1","b2"," c3]]
        var a_to_d = my2dimArray[0]
        //or

        var steakSauce = [[a,b,c,d],[1,2,3,4],["a1","b2"," c3]][2][0]

        Mick

        Comment

        • Grant Wagner

          #5
          Re: Array element assignment

          "Berislav Lopac" <berislav.lopac @lopsica.com> wrote in message
          news:ctas00$7vg $1@garrison.glo balnet.hr...[color=blue]
          > Does anyone has experience/information about the performance of the
          > array elements assignment?
          >
          > Specifically, which is more efficient:
          >
          > var myArray = new Array(a, b, c, d);
          >
          > or
          >
          > var myArray = new Array();
          > myArray[] = a;
          > myArray[] = b;
          > myArray[] = c;
          > myArray[] = d;
          >
          >
          > Specifically, I'm talking about multi-dimensional arrays with, all
          > together, several thousand elements in them.[/color]

          I'm on a roll with this today, but, and I'll say it again: if you're
          asking this sort of question, you're using the wrong tool for the job.

          <url: http://blogs.msdn.com/ericlippert/ar.../18/53388.aspx />

          "High performance is unimportant -- as long as the page doesn't appear
          to hang, its fast enough. "

          "Do not rely on "tips and tricks" for performance. People will tell you
          "declared variables are faster than undeclared variables" and "modulus
          is slower than bit shift" and all kinds of nonsense. Ignore them.
          That's like mowing your lawn by going after random blades of grass with
          nail scissors. You need to find the WORST thing, and fix it first.
          That means measuring. Get some tools -- Visual Studio Analyzer can do
          some limited script profiling, as can the Numega script profiler, but
          even just putting some logging into the code that dumps out millisecond
          timings is a good way to start. Once you know what the slowest thing
          is, you can concentrate on modularizing and fixing it. "

          <script type="text/javascript">
          var t = (new Date()).getTime ();
          for (var count = 0; count < 1000; ++count)
          {
          var a = new Array(10);
          var ii = a.length;
          while (ii-- > 0)
          {
          a[ii] = new Array(10);
          var jj = a[ii].length;
          while (jj-- > 0)
          {
          a[ii][jj] = 9e9;
          }
          }
          }
          document.write( ((new Date()).getTime () - t) + '<br>');
          var t = (new Date()).getTime ();
          for (var count = 0; count < 1000; ++count)
          {
          var a = new Array(
          new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
          new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
          new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
          new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
          new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
          new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
          new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
          new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
          new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
          new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9)
          );
          }
          document.write( ((new Date()).getTime () - t) + '<br>');
          </script>

          Creating 1000 - 10 x 10 arrays takes less than a second on my machine
          either way. What difference does it make if one takes 1/4 of a second
          and the other takes 1/2 second. Both are faster than the human being
          looking at the Web page. Write mantainable code.

          --
          Grant Wagner <gwagner@agrico reunited.com>
          comp.lang.javas cript FAQ - http://jibbering.com/faq


          Comment

          • Dr John Stockton

            #6
            Re: Array element assignment

            JRS: In article <ctas00$7vg$1@g arrison.globaln et.hr>, dated Thu, 27 Jan
            2005 14:57:21, seen in news:comp.lang. javascript, Berislav Lopac
            <berislav.lopac @lopsica.com> posted :[color=blue]
            >Does anyone has experience/information about the performance of the array
            >elements assignment?
            >
            >Specifically , which is more efficient:[/color]
            [color=blue]
            > ...[/color]

            You can measure the execution time in the browser(s) that you use; if
            the difference(s) are not significant in comparison for the time taken
            for everything, then they don't matter for you and probably not for
            anyone else.

            For example, I've used

            function Timer() { var J, K, M=30, N=(50000/M)|0, D0, D1, D2, D3
            D0 = new Date()
            K = N ; while (--K) { J = M ; while (--J) { } }
            D1 = new Date()
            K = N ; while (--K) { J = M ; while (--J) lz(J) }
            D2 = new Date()
            K = N ; while (--K) { J = M ; while (--J) LZ(J) }
            D3 = new Date()
            alert('Nul ' + (D1-D0) + ', lz ' + (D2-D1) + ', LZ ' + (D3-D2)) }

            to compare

            function LZ(x) { return (x<0||x>=10?"": "0") + x }

            function lz(x) { var t = String(x)
            return t.length==1 ? "0"+t : t }

            with the result that LZ seems about 25% better.

            Those with faster computers may need to increase the big number.

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
            <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
            <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

            Comment

            • Randell D.

              #7
              Re: Array element assignment

              Grant Wagner wrote:
              [color=blue]
              > "Berislav Lopac" <berislav.lopac @lopsica.com> wrote in message
              > news:ctas00$7vg $1@garrison.glo balnet.hr...
              >[color=green]
              >>Does anyone has experience/information about the performance of the
              >>array elements assignment?
              >>
              >>Specificall y, which is more efficient:
              >>
              >>var myArray = new Array(a, b, c, d);
              >>
              >>or
              >>
              >>var myArray = new Array();
              >>myArray[] = a;
              >>myArray[] = b;
              >>myArray[] = c;
              >>myArray[] = d;
              >>
              >>
              >>Specificall y, I'm talking about multi-dimensional arrays with, all
              >>together, several thousand elements in them.[/color]
              >
              >
              > I'm on a roll with this today, but, and I'll say it again: if you're
              > asking this sort of question, you're using the wrong tool for the job.
              >
              > <url: http://blogs.msdn.com/ericlippert/ar.../18/53388.aspx />
              >
              > "High performance is unimportant -- as long as the page doesn't appear
              > to hang, its fast enough. "
              >
              > "Do not rely on "tips and tricks" for performance. People will tell you
              > "declared variables are faster than undeclared variables" and "modulus
              > is slower than bit shift" and all kinds of nonsense. Ignore them.
              > That's like mowing your lawn by going after random blades of grass with
              > nail scissors. You need to find the WORST thing, and fix it first.
              > That means measuring. Get some tools -- Visual Studio Analyzer can do
              > some limited script profiling, as can the Numega script profiler, but
              > even just putting some logging into the code that dumps out millisecond
              > timings is a good way to start. Once you know what the slowest thing
              > is, you can concentrate on modularizing and fixing it. "
              >
              > <script type="text/javascript">
              > var t = (new Date()).getTime ();
              > for (var count = 0; count < 1000; ++count)
              > {
              > var a = new Array(10);
              > var ii = a.length;
              > while (ii-- > 0)
              > {
              > a[ii] = new Array(10);
              > var jj = a[ii].length;
              > while (jj-- > 0)
              > {
              > a[ii][jj] = 9e9;
              > }
              > }
              > }
              > document.write( ((new Date()).getTime () - t) + '<br>');
              > var t = (new Date()).getTime ();
              > for (var count = 0; count < 1000; ++count)
              > {
              > var a = new Array(
              > new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
              > new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
              > new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
              > new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
              > new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
              > new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
              > new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
              > new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
              > new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9),
              > new Array(9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9, 9e9)
              > );
              > }
              > document.write( ((new Date()).getTime () - t) + '<br>');
              > </script>
              >
              > Creating 1000 - 10 x 10 arrays takes less than a second on my machine
              > either way. What difference does it make if one takes 1/4 of a second
              > and the other takes 1/2 second. Both are faster than the human being
              > looking at the Web page. Write mantainable code.
              >[/color]

              I've got about six months experience with js and agree for the most part
              with your arguements - however I would recommend

              var myArray = new Array(a, b, c, d);

              instead of

              var myArray = new Array();
              myArray[] = a;
              myArray[] = b;
              myArray[] = c;
              myArray[] = d;


              The OP mentions they will have several thousands of elements - If this
              is true, using the former method would at very least mean less data over
              the wire. Small gains - but as the saying goes - look after the pennies
              and the pounds will look after themselves...

              randelld

              Comment

              • Berislav Lopac

                #8
                Re: Array element assignment

                Grant Wagner wrote:[color=blue]
                > "High performance is unimportant -- as long as the page doesn't appear
                > to hang, its fast enough. "[/color]

                Actually, it does, on some machines (I didn't get the specs, but my guess
                they are not top of the line).

                Berislav


                Comment

                Working...