jQuery append vs html vs innerHTML list performance (v112)

Revision 112 of this benchmark created on


Description

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

Preparation HTML

<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>

Setup

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>';
    
    }

Test runner

Ready to run.

Testing in
TestOps/sec
jquery .append()
  $('#list').append(html.join(''));
  $('#list1').append(html.join(''));
  $('#list2').append(html.join(''));
  $('#list3').append(html.join(''));
  $('#list4').append(html.join(''));
ready
jQuery .html()
  $('#list').html(htmlOption );
$('#list1').html(htmlOption );
$('#list2').html(htmlOption );
$('#list3').html(htmlOption );
$('#list4').html(htmlOption );

 
ready
javascript innerHTML
 $('#list').append(htmlOption );
 $('#list1').append(htmlOption );
 $('#list2').append(htmlOption );
 $('#list3').append(htmlOption );
 $('#list4').append(htmlOption );
 
ready
Concat
 $('#list').html(html.join(''));
 $('#list1').html(html.join(''));
 $('#list2').html(html.join(''));
 $('#list3').html(html.join(''));
 $('#list4').html(html.join(''));
ready
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);
                     }
ready

Revisions

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