.append VS .html VS [removed] performance

前端 未结 7 1648
星月不相逢
星月不相逢 2020-11-28 21:16

This site\'s run a test between the 3 different methods and it seems .html is the fastest, followed by .append. followed by .innerHTML

相关标签:
7条回答
  • 2020-11-28 21:40

    I think the innerHTML is faster with suggesstion @Brat.

    And on creating loop and appending string should be good on using variable first. It is make your performance more good.

    good code:

    var html = '';
    for (var i = 0; i < len; i++) {
      html += '<div>Test ' + i + '</div>';
    };
    $('#list').append(html);
    

    not efficient code:

    for (var i = 0; i < len; i++) {
      var html = '<div>Test ' + i + '</div>';
      $('#list').append(html);
    }
    

    for example: http://jsben.ch/#/yDvKH

    0 讨论(0)
  • 2020-11-28 21:42

    That benchmark is worthless. innerHTML is always faster than DOM manipulation.

    jQuery seems faster because it prepares a string with all the HTML first while the others do one operation each iteration. Also note that jQuery.html() uses innerHTML whenever it can.

    jQuery from benchmark

    var html = '';
    for (var i = 0; i < len; i++) {
      html += '<div>Test ' + i + '</div>';
    }
    
    $('#list').html(html);
    

    innerHTML from benchmark

    var list = document.getElementById('list');
    for (var i = 0; i < len; i++) {
      list.innerHTML = list.innerHTML + '<div>Test ' + i + '</div>';
    }
    

    The test for innerHTML would be a lot faster if it was written like:

    var list = document.getElementById('list');
    var html = '';
    
    for (var i = 0; i < len; i++) {
        html += '<div>Test ' + i + '</div>';
    }
    
    list.innerHTML = html;
    

    http://jsben.ch/#/yDvKH

    0 讨论(0)
  • 2020-11-28 21:43

    I also had a problem with big table redraw (about 10x100 size). It takes about 300ms to redraw whole table.

    The reason was not in the jQuery.append() and not in dom.innerHTML, but in appending each element each time.

    The fastest way is to concatenate all elements html code and then append it to DOM. Like this:

    function redrawMyTable( myData )
    {
        var innerHTML = '';
        for ( var i = 0; i < myData.length; i++ )
        {
          innerHTML += createRowFromData( myData[i] );
        }
    
        myTableTbody.innerHTML = innerHTML;
    }
    function createRowFromData( rowData )
    {
        var rowHTML = '';
        for ( var i = 0; i < rowData.length; i++ )
        {
          rowHTML += createCellFromData( rowData[i] );
        }
        return rowHTML;
    }
    function createCellFromData( cellData )
    {
        //Do everything you need, and return HTMl code as a string
        return cellHTML;
    }
    

    Now it takes only 20-30 ms (against 300ms :))

    0 讨论(0)
  • 2020-11-28 21:48

    All three are slow to me. Modifying the dom on each iteration is slow.

    http://jsperf.com/jquery-append-vs-html-list-performance/24

    I just added a new test in there:

    var html = [];
    for (var i = 0; i < len; i++) {
      html.push('<div>Test ' + i + '</div>');
    }
    
    document.getElementById('list').innerHTML = html.join('');
    

    This is much faster again. :)

    My method in Firefox does 26k Ops/sec vs 1,000, 10,000, and 13

    enter image description here

    0 讨论(0)
  • 2020-11-28 21:50

    As Bart said, innerHTML is always faster than DOM manipulation.

    I was testing hyperHTML, so I thought I share my results. I didn't actually run my benchmarks in CodePen originally, and there is an interesting difference in that the jQuery times are much closer to innerHTML running in CodePen.

    Chrome:
    createFragment 312.80 ms  
    hyperHTML      253.10 ms     
    innerHTML       62.70 ms   
    $.append       183.40 ms
    
    Chrome (extensions off): 
    createFragment 225.10 ms 
    hyperHTML      139.80 ms 
    innerHTML       47.80 ms 
    $.append       170.90 ms
    
    Firefox: 
    createFragment 141 ms 
    hyperHTML       84 ms 
    innerHTML       25 ms 
    $.append        90 ms
    
    Edge: 
    createFragment 422.50 ms 
    hyperHTML      184.60 ms 
    innerHTML       44.00 ms 
    $.append      1629.69 ms
    
    IE11: 
    createFragment   1180.29 ms 
    hyperHTML       13315.59 ms //slow fallbacks, IE sucks 
    innerHTML         125.70 ms 
    $.append         2382.49 ms
    

    I think it is all pretty simple. JavaScript is not as fast as the browser at parsing and creating elements, because the browser is machine specific compiled code. You can't do better than just handing over the HTML and letting the browser do the work without interruption.

    It is possible that some of the performance differences are due to XSS checking, which would seem prudent.

    function runbench(){
      var data = [];
      for (var i = 0; i < 10001; i++) {
          data.push("<span>" + i + "</span>");
      }
    
      var perf=[];
      var t0 = performance.now();
      var c = document.createDocumentFragment();
      for (var i = 0; i < 10001; i++) {
          var e = document.createElement("span");
          e.innerHTML = data[i];
          c.appendChild(e);
      }
      document.querySelector('#createFragment').appendChild(c);
      document.querySelector('#createFragment').classList='done';
      var t1 = performance.now();
      perf.push(t1-t0);
    
      var t0 = performance.now();
      document.querySelector('#innerHTML').innerHTML = data.join('');
      document.querySelector('#innerHTML').classList='done';
      var t1 = performance.now();
      perf.push(t1-t0);
    
      var t0 = performance.now();
      $('#jqhtml').html(data.join(''));
      document.querySelector('#jqhtml').classList='done';
      var t1 = performance.now();
      perf.push(t1-t0);
    
      var t0 = performance.now();
      $('#jqappend').append(data.join(''));
      document.querySelector('#jqappend').classList='done';
      var t1 = performance.now();
      perf.push(t1-t0);
    
      var t0 = performance.now();
      hyperHTML.bind(document.querySelector('#hyperHTML'))       
      `${data.map(function (item) {
          return "<span>" + item + "</span>";
      })}`;
      document.querySelector('#hyperHTML').classList='done';
      var t1 = performance.now();
      perf.push(t1-t0);
    
      var stats = [];
      stats.push("<table>")
      stats.push("<tr><td>createFrag: </td><td>" + perf[0].toFixed(2) + "</td></tr>");
      stats.push("<tr><td>innerHTML: </td><td>" + perf[1].toFixed(2) + "</td></tr>");
      stats.push("<tr><td>$.html: </td><td>" + perf[2] .toFixed(2) + "</td></tr>");
      stats.push("<tr><td>$.append: </td><td>" + perf[3] .toFixed(2) + "</td></tr>");
      stats.push("<tr><td>hyperHTML: </td><td>" + perf[4].toFixed(2) + "</td></tr>");
      stats.push("</table>");
      $('#performance').html(stats.join(''));
      document.querySelector('#performance').classList='done';
    }
    

    https://codepen.io/jwhooper/pen/GzKwMV

    0 讨论(0)
  • 2020-11-28 21:56

    How can .html be faster than .innerHTML when the .html is using .innerHTML with a lot of extra code? Here .html implementation in jQuery (taken directly from jQuery file).

    html: function( value ) {
        return jQuery.access( this, function( value ) {
            var elem = this[0] || {},
                i = 0,
                l = this.length;
    
            if ( value === undefined ) {
                return elem.nodeType === 1 ?
                    elem.innerHTML.replace( rinlinejQuery, "" ) :
                    undefined;
            }
    
            // See if we can take a shortcut and just use innerHTML
            if ( typeof value === "string" && !rnoInnerhtml.test( value ) &&
                ( jQuery.support.htmlSerialize || !rnoshimcache.test( value )  ) &&
                ( jQuery.support.leadingWhitespace || !rleadingWhitespace.test( value ) ) &&
                !wrapMap[ ( rtagName.exec( value ) || ["", ""] )[1].toLowerCase() ] ) {
    
                value = value.replace( rxhtmlTag, "<$1></$2>" );
    
                try {
                    for (; i < l; i++ ) {
                        // Remove element nodes and prevent memory leaks
                        elem = this[i] || {};
                        if ( elem.nodeType === 1 ) {
                            jQuery.cleanData( getAll( elem, false ) );
                            elem.innerHTML = value;
                        }
                    }
    
                    elem = 0;
    
                // If using innerHTML throws an exception, use the fallback method
                } catch(e) {}
            }
    
            if ( elem ) {
                this.empty().append( value );
            }
        }, null, value, arguments.length );
    }
    
    0 讨论(0)
提交回复
热议问题