jQuery append vs html vs innerHTML list performance

JavaScript performance comparison

Revision 132 of this test case created by C E

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<div id="list" style="display:none">
</div>
      
<script>
Benchmark.prototype.setup = function() {
  var html = '';
  
  for (var i = 0; i < 20; i++) {
    html += '<div>Test ' + i + '</div>';
  }
  
  var htmlArray = [];
  
  for (var i = 0; i < 20; i++) {
    htmlArray[i] = '<div>Test ' + i + '</div>';
  }

};
</script>

Preparation code output

<div id="list" style="display:none"> </div>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
jQuery .append() at end
$('#list').append(html);
pending…
jQuery .html() at end
$('#list').html(html);
pending…
javascript innerHTML at end
document.getElementById('list').innerHTML = html;
pending…
javascript innerHTML at end (including string concat time)
var html2 = '';

for (var i = 0; i < 20; i++) {
  html2 += '<div>Test ' + i + '</div>';
}

document.getElementById('list').innerHTML = html2;
pending…
jQuery .append() in loop
for (var i = 0; i < 20; i++) {
  $('#list').append('<div>Test ' + i + '</div>');
}
pending…
jQuery .html() in loop
for (var i = 0; i < 20; i++) {
  $('#list').html('<div>Test ' + i + '</div>');
}
pending…
javascript innerHTML in loop
var list = document.getElementById('list')

var html2 = '';
for (var i = 0; i < 20; i++) {
  list.innerHTML += '<div>Test ' + i + '</div>';
}
pending…
jQuery append($) at end
$('#list').append($(html));
pending…
jQuery append(array) at end
$('#list').append(htmlArray);
pending…
jQuery append(array) at end, showing array build time
var htmlArray2 = [];

for (var i = 0; i < 20; i++) {
  htmlArray2[i] = '<div>Test ' + i + '</div>';
}

$('#list').append(htmlArray2);
pending…
javascript innerHTML at end (including array push time)
var html3 = [];

for (var i = 0; i < 20; i++) {
  html3.push('<div>Test ' + i + '</div>');
}

document.getElementById('list').innerHTML = html3.join('');
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments