jQuery append vs html vs innerHTML list performance

JavaScript performance comparison

Revision 112 of this test case created

Info

Just a simple example showing the speed difference between populating a list with .append() & .html() and innerHTML

Preparation code

<script src="//code.jquery.com/jquery-1.9.1.min.js">
</script>
<select id="list" ></select>
<select id="list1" ></select>
<select id="list2" ></select>
<select id="list3" ></select>
<select id="list4" ></select>
<script>
Benchmark.prototype.setup = function() {
    var len = 5000;
    var html=[];
    for (var i = 0; i < len; i++) {
      html.push('<option value="'+ i +'">Test' + i + '</option>');
   
    }
   
    var htmlArrayObjects =[];
    for (var i = 0; i < len; i++) {
      html.push({label:'Test' + i,value:i}
    );
   
    }
   
   
    var htmlOption = '';
    for (var a = 0; a < len; a++) {
      htmlOption = htmlOption + '<option value="'+ a +'">Test' + a + '</option>';
   
    }
   
};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
jquery .append()
  $('#list').append(html.join(''));
  $('#list1').append(html.join(''));
  $('#list2').append(html.join(''));
  $('#list3').append(html.join(''));
  $('#list4').append(html.join(''));
pending…
jQuery .html()
  $('#list').html(htmlOption );
$('#list1').html(htmlOption );
$('#list2').html(htmlOption );
$('#list3').html(htmlOption );
$('#list4').html(htmlOption );

 
pending…
javascript innerHTML
 $('#list').append(htmlOption );
 $('#list1').append(htmlOption );
 $('#list2').append(htmlOption );
 $('#list3').append(htmlOption );
 $('#list4').append(htmlOption );
 
pending…
Concat
 $('#list').html(html.join(''));
 $('#list1').html(html.join(''));
 $('#list2').html(html.join(''));
 $('#list3').html(html.join(''));
 $('#list4').html(html.join(''));
pending…
Manually new Options
for(i = 0; i < htmlArrayObjects.length; i++){
document.getElementById("list").options[i] = new Option(htmlArrayObjects.label,htmlArrayObjects.value);
                     }
for(i = 0; i < htmlArrayObjects.length; i++){
document.getElementById("list1").options[i] = new Option(htmlArrayObjects.label,htmlArrayObjects.value);
                     }
for(i = 0; i < htmlArrayObjects.length; i++){
document.getElementById("list2").options[i] = new Option(htmlArrayObjects.label,htmlArrayObjects.value);
                     }
for(i = 0; i < htmlArrayObjects.length; i++){
document.getElementById("list3").options[i] = new Option(htmlArrayObjects.label,htmlArrayObjects.value);
                     }
for(i = 0; i < htmlArrayObjects.length; i++){
document.getElementById("list4").options[i] = new Option(htmlArrayObjects.label,htmlArrayObjects.value);
                     }
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. Here’s a list of current revisions for this page:

0 comments

Add a comment