DOM table building

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Christopher Benson-Manica

    #1

    DOM table building

    Why is building a table with the DOM slower than using an array? IOW,
    why is

    var table=document. createDocumentF ragment();
    for( var i=0; i < 4000; i++ ) {
    tr=table.insert Row( table.rows.leng th );
    td=tr.appendChi ld( document.create Element('td') );
    td.appendChild( 'foo' );
    }
    someElement.app endChild( table );

    much slower than

    var table[];
    table.push( "<table>" );
    for( var i=0; i < 4000; i++ ) {
    table.push( "<tr>" );
    table.push( "<td>foo</td>" );
    table.push( "</tr>" );
    }
    table.push( "</table>" );
    document.writel n( table.join('') ); // thanks again for the tip!!

    ? Am I doing something inadvisable with the document object?

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
  • RobG

    #2
    Re: DOM table building

    Christopher Benson-Manica wrote:[color=blue]
    > Why is building a table with the DOM slower than using an array? IOW,
    > why is[/color]

    It depends on the browser. In Safari, DOM is faster (or innerHTML is
    more sluggish...)
    [color=blue]
    >
    > var table=document. createDocumentF ragment();
    > for( var i=0; i < 4000; i++ ) {
    > tr=table.insert Row( table.rows.leng th );[/color]

    This is faster as:

    tr=table.insert Row(i);
    [color=blue]
    > td=tr.appendChi ld( document.create Element('td') );
    > td.appendChild( 'foo' );[/color]

    This line created an error in Firefox and IE, use:

    td.appendChild( document.create TextNode('foo') );

    [...][color=blue]
    > ? Am I doing something inadvisable with the document object?[/color]

    Only in regard to unnecessarily calculating the number of rows on each
    iteration - just use the counter, "i".

    My results (times approx in milliseconds):

    Browser DOM innerHTML
    Firefox 4,200 350
    IE 40,000 200

    I could not believe the IE time, so I ran the test several times (code
    below)

    From the results I surmise that Microsoft has no interest in making DOM
    methods more efficient. This is a rather narrow minded view, since in
    IE you can't build part of a table using innerHTML, you must build all
    or nothing. So if you want to replace a row, you must use their
    sluggish DOM method - which Firefox will run 10 times faster.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <html>
    <head>
    <title>demo</title>
    <script type="text/javascript">
    function doTable(){
    var now = new Date();
    var table = document.create Element('table' );
    table.style.bor der = "1px solid blue";
    for( var i=0; i < 4000; i++ ) {
    tr=table.insert Row(i);
    td=tr.appendChi ld( document.create Element('td') );
    td.appendChild( document.create TextNode('foo') );
    }

    document.getEle mentById('dom') .appendChild( table );

    var now2 = new Date();
    var x = now2.getTime() - now.getTime();
    var msg = 'DOM way: ' + x;

    var now = new Date();
    var table = [
    "<table style='border: 1px",
    " solid red;'>",
    "<tbody>"
    ];
    for( var i=0; i < 4000; i++ ) {
    table.push( "<tr><td>fo o</td></tr>" );
    }
    table.push( "</tbody></table>" );

    document.getEle mentById('inner ').innerHTML = table.join('');
    var now2 = new Date();
    var x = now2.getTime()-now.getTime()
    msg += '\ninnerHTML way: ' + x;
    alert(msg);
    }
    </script>
    </head>
    <body onload="doTable ();">
    <div id="dom"></div>
    <div id="inner"></div>
    </body>
    </html>


    --
    Rob

    Comment

    • Andrew Poulos

      #3
      Re: DOM table building

      RobG wrote:
      [snip][color=blue]
      >
      > My results (times approx in milliseconds):
      >
      > Browser DOM innerHTML
      > Firefox 4,200 350
      > IE 40,000 200
      >
      > I could not believe the IE time, so I ran the test several times (code
      > below)
      >
      > From the results I surmise that Microsoft has no interest in making DOM
      > methods more efficient. This is a rather narrow minded view, since in
      > IE you can't build part of a table using innerHTML, you must build all
      > or nothing. So if you want to replace a row, you must use their
      > sluggish DOM method - which Firefox will run 10 times faster.
      >
      > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
      > <html>
      > <head>
      > <title>demo</title>
      > <script type="text/javascript">
      > function doTable(){
      > var now = new Date();
      > var table = document.create Element('table' );
      > table.style.bor der = "1px solid blue";
      > for( var i=0; i < 4000; i++ ) {
      > tr=table.insert Row(i);
      > td=tr.appendChi ld( document.create Element('td') );
      > td.appendChild( document.create TextNode('foo') );
      > }
      >
      > document.getEle mentById('dom') .appendChild( table );
      >
      > var now2 = new Date();
      > var x = now2.getTime() - now.getTime();
      > var msg = 'DOM way: ' + x;
      >
      > var now = new Date();
      > var table = [
      > "<table style='border: 1px",
      > " solid red;'>",
      > "<tbody>"
      > ];
      > for( var i=0; i < 4000; i++ ) {
      > table.push( "<tr><td>fo o</td></tr>" );
      > }
      > table.push( "</tbody></table>" );
      >
      > document.getEle mentById('inner ').innerHTML = table.join('');
      > var now2 = new Date();
      > var x = now2.getTime()-now.getTime()
      > msg += '\ninnerHTML way: ' + x;
      > alert(msg);
      > }
      > </script>
      > </head>
      > <body onload="doTable ();">
      > <div id="dom"></div>
      > <div id="inner"></div>
      > </body>
      > </html>
      >
      >[/color]
      I edited your code to this:


      var table = document.create Element('table' );
      table.style.bor der = "1px solid blue";
      var b = document.create Element("tbody" );

      for( var i = 0; i < 4000; i++ ) {
      tr = document.create Element("tr");
      td = tr.appendChild( document.create Element('td') );
      td.appendChild( document.create TextNode('foo') );
      b.appendChild(t r);
      }

      table.appendChi ld(b);
      document.getEle mentById('dom') .appendChild( table );


      And got the DOM response from IE6 on WinXP SP2 to under 1,000ms. The
      innerHTML response was under 200ms.

      Andrew Poulos

      Comment

      • RobG

        #4
        Re: DOM table building

        Andrew Poulos wrote:
        [...][color=blue]
        > var table = document.create Element('table' );
        > table.style.bor der = "1px solid blue";
        > var b = document.create Element("tbody" );
        >
        > for( var i = 0; i < 4000; i++ ) {
        > tr = document.create Element("tr");
        > td = tr.appendChild( document.create Element('td') );[/color]

        I guess strictly speaking this should be:

        var tr = document.create Element('tr');
        var td = tr.appendChild( document.create Element('td') );

        but reducing the scope to local makes no difference to the speed that I
        can tell.
        [color=blue]
        > td.appendChild( document.create TextNode('foo') );
        > b.appendChild(t r);
        > }
        >
        > table.appendChi ld(b);
        > document.getEle mentById('dom') .appendChild( table );
        >
        >
        > And got the DOM response from IE6 on WinXP SP2 to under 1,000ms. The
        > innerHTML response was under 200ms.
        >[/color]

        Very similar results here (Firefox is still twice as fast as IE).

        It seems explicitly creating the tbody, rather than allowing the
        browser to create it by inference, saves a lot of time. I can only
        guess that the modifications required to the tbody each time the tr is
        added to the table directly takes much more effort than adding to the
        tbody, then (ultimately) to the table. But four times the effort for
        Firefox or 20 times the effort for IE seems extreeme when most
        programmers don't put tbodys in their tables (yeah, I know the spec
        says they should be there, maybe browsers are too tolerant).

        Anyone else with an optimisation?


        --
        Rob

        Comment

        • Kiran Makam

          #5
          Re: DOM table building

          I applied Duff's Device ( used his technique instead of normal for
          loop,
          more info available at
          http://homepage.mac.com/rue/JS_Optimization_Techniques/ )

          Results were amazing in IE:
          Normal for loop:1235 milli secs
          Duff's Device: 156 milli secs

          Here is the code:

          <div id=dom></div>
          <script>
          var start = new Date();

          var table = document.create Element('table' );
          table.style.bor der = "1px solid blue";
          var b = document.create Element("tbody" );

          var iterations = 4000
          var testVal=0;
          var n = iterations % 8;

          if (n>0) {
          do
          {
          testVal++;
          }
          while (--n); // n must be greater than 0 here
          }

          n = parseInt(iterat ions / 8);
          do
          {
          tr = document.create Element("tr");
          td = tr.appendChild( document.create Element('td') );
          td.appendChild( document.create TextNode('foo') );
          b.appendChild(t r);
          }
          while (--n);


          table.appendChi ld(b);
          document.getEle mentById('dom') .appendChild( table );
          var end = new Date();
          document.write( (end - start) );
          </script>

          Comment

          • RobG

            #6
            Re: DOM table building

            Kiran Makam wrote:[color=blue]
            > I applied Duff's Device ( used his technique instead of normal for
            > loop,
            > more info available at
            > http://homepage.mac.com/rue/JS_Optimization_Techniques/ )
            >
            > Results were amazing in IE:
            > Normal for loop:1235 milli secs
            > Duff's Device: 156 milli secs[/color]

            I'm not surprised. Let's see, 1235/156 = 7.9 ... pretty close to 8.
            [color=blue]
            >
            > Here is the code:
            >
            > <div id=dom></div>
            > <script>
            > var start = new Date();
            >
            > var table = document.create Element('table' );
            > table.style.bor der = "1px solid blue";
            > var b = document.create Element("tbody" );
            >
            > var iterations = 4000
            > var testVal=0;
            > var n = iterations % 8;[/color]

            Now why would I want iterations mod 8?
            [color=blue]
            >
            > if (n>0) {
            > do
            > {
            > testVal++;
            > }
            > while (--n); // n must be greater than 0 here
            > }
            >
            > n = parseInt(iterat ions / 8);[/color]

            More to the point, n is now iterations *divided* by 8...
            [color=blue]
            > do
            > {
            > tr = document.create Element("tr");
            > td = tr.appendChild( document.create Element('td') );
            > td.appendChild( document.create TextNode('foo') );
            > b.appendChild(t r);
            > }
            > while (--n);[/color]

            So it only iterates 4000/8 = 500 times.

            modify the createTextNode line to be:

            td.appendChild( document.create TextNode('foo ' + ' : ' + n));

            and you will see that only 500 cells/rows are written to the table,
            hence the magical 8 fold increase in speed. More remarkable is that
            the reduction can be entire blamed on the reduction in the number of
            elements created.

            QED

            Sorry to burst your bubble.

            --
            Rob

            Comment

            • Grant Wagner

              #7
              Re: DOM table building

              "Andrew Poulos" <ap_prog@hotmai l.com> wrote in message
              news:41ec5508$0 $16239$5a62ac22 @per-qv1-newsreader-01.iinet.net.au ...[color=blue]
              > RobG wrote:
              > [snip][color=green]
              >>
              >> My results (times approx in milliseconds):
              >>
              >> Browser DOM innerHTML
              >> Firefox 4,200 350
              >> IE 40,000 200
              >>
              >> I could not believe the IE time, so I ran the test several times
              >> (code
              >> below)
              >>
              >> From the results I surmise that Microsoft has no interest in making
              >> DOM
              >> methods more efficient. This is a rather narrow minded view, since
              >> in
              >> IE you can't build part of a table using innerHTML, you must build
              >> all
              >> or nothing. So if you want to replace a row, you must use their
              >> sluggish DOM method - which Firefox will run 10 times faster.
              >>
              >> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
              >> <html>
              >> <head>
              >> <title>demo</title>
              >> <script type="text/javascript">
              >> function doTable(){
              >> var now = new Date();
              >> var table = document.create Element('table' );
              >> table.style.bor der = "1px solid blue";
              >> for( var i=0; i < 4000; i++ ) {
              >> tr=table.insert Row(i);
              >> td=tr.appendChi ld( document.create Element('td') );
              >> td.appendChild( document.create TextNode('foo') );
              >> }
              >>
              >> document.getEle mentById('dom') .appendChild( table );
              >>
              >> var now2 = new Date();
              >> var x = now2.getTime() - now.getTime();
              >> var msg = 'DOM way: ' + x;
              >>
              >> var now = new Date();
              >> var table = [
              >> "<table style='border: 1px",
              >> " solid red;'>",
              >> "<tbody>"
              >> ];
              >> for( var i=0; i < 4000; i++ ) {
              >> table.push( "<tr><td>fo o</td></tr>" );
              >> }
              >> table.push( "</tbody></table>" );
              >>
              >> document.getEle mentById('inner ').innerHTML = table.join('');
              >> var now2 = new Date();
              >> var x = now2.getTime()-now.getTime()
              >> msg += '\ninnerHTML way: ' + x;
              >> alert(msg);
              >> }
              >> </script>
              >> </head>
              >> <body onload="doTable ();">
              >> <div id="dom"></div>
              >> <div id="inner"></div>
              >> </body>
              >> </html>
              >>
              >>[/color]
              > I edited your code to this:
              >
              >
              > var table = document.create Element('table' );
              > table.style.bor der = "1px solid blue";
              > var b = document.create Element("tbody" );
              >
              > for( var i = 0; i < 4000; i++ ) {
              > tr = document.create Element("tr");
              > td = tr.appendChild( document.create Element('td') );
              > td.appendChild( document.create TextNode('foo') );
              > b.appendChild(t r);
              > }
              >
              > table.appendChi ld(b);
              > document.getEle mentById('dom') .appendChild( table );
              >
              >
              > And got the DOM response from IE6 on WinXP SP2 to under 1,000ms. The
              > innerHTML response was under 200ms.[/color]

              If you really are creating a table with one (or even many) columns and
              they all contain the same text, then it takes about 1/2 the time to
              execute:

              var tr = document.create Element("tr");
              var td = tr.appendChild( document.create Element('td'));
              td.appendChild( document.create TextNode('foo') );
              for (var i = 0; i < 4000; i++) {
              b.appendChild(t r.cloneNode(tru e));
              }

              Create the row and the cell(s) once, then cloneNode() them. I'd imagine
              this would actually get faster the more you called
              document.create Element()/createTextNode( ) inside the loop (ie - tables
              with more columns would benefit more from this algorithm).

              Certainly cloneNode() is just walking the DOM hierarchy of the node
              you're cloning and doing what you would do anyway, but it's under the
              control of the script engine, not your script, so it should run faster
              (the same way Array#join() is faster than manually joining an Array).

              Of course I'm not sure how realistic it is to assume you'd be creating a
              4000 row table with all rows identical.

              Who knows though. It might be faster to create a 4000 row x 20 column
              table using cloneNode(), then come back and change the nodeValues to
              what they are supposed to be. You'd have to test it.

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


              Comment

              • Andrew Poulos

                #8
                Re: DOM table building

                Grant Wagner wrote:

                [snip]
                [color=blue]
                > If you really are creating a table with one (or even many) columns and
                > they all contain the same text, then it takes about 1/2 the time to
                > execute:
                >
                > var tr = document.create Element("tr");
                > var td = tr.appendChild( document.create Element('td'));
                > td.appendChild( document.create TextNode('foo') );
                > for (var i = 0; i < 4000; i++) {
                > b.appendChild(t r.cloneNode(tru e));
                > }
                >
                > Create the row and the cell(s) once, then cloneNode() them. I'd imagine
                > this would actually get faster the more you called
                > document.create Element()/createTextNode( ) inside the loop (ie - tables
                > with more columns would benefit more from this algorithm).
                >
                > Certainly cloneNode() is just walking the DOM hierarchy of the node
                > you're cloning and doing what you would do anyway, but it's under the
                > control of the script engine, not your script, so it should run faster
                > (the same way Array#join() is faster than manually joining an Array).
                >
                > Of course I'm not sure how realistic it is to assume you'd be creating a
                > 4000 row table with all rows identical.
                >
                > Who knows though. It might be faster to create a 4000 row x 20 column
                > table using cloneNode(), then come back and change the nodeValues to
                > what they are supposed to be. You'd have to test it.
                >[/color]

                I tested creating the table using cloneNode and averaged these values
                (in milliseconds):

                Browser DOM innerHTML
                Firefox 430 280
                IE 625 150

                Andrew Poulos


                Comment

                Working...