Document fragment vs innerHTML vs looped appendChild (v9)

Revision 9 of this benchmark created on


Preparation HTML

<div id="my-svg">
</div>

Setup

var div = document.getElementById('my-svg')
  var svgNS="http://www.w3.org/2000/svg";
  var elmSvg = document.createElementNS(svgNS,"svg");

Teardown



            while(elmSvg.firstChild)elmSvg.removeChild(elmSvg.firstChild);
  while(div.firstChild)div.removeChild(div.firstChild);
        
  

Test runner

Ready to run.

Testing in
TestOps/sec
innerHTML
var i = 0;
var htmlText = '';
while (i < 20) {
  htmlText += '<rect> </rect>';
  i++;
}
elmSvg.innerHTML = htmlText ;
div.appendChild(elmSvg);
ready
Looped appendChild
var i = 0;

while (i < 20) {
  var el = document.createElementNS(svgNS,'rect');
  elmSvg.appendChild(el);
  i++;
}

div.appendChild(elmSvg);
ready
Document fragment
var i = 0,
  fragment = document.createDocumentFragment();

while (i < 20) {
  var el = document.createElementNS(svgNS,'rect');
  fragment.appendChild(el);
  i++;
}

elmSvg.appendChild(fragment);
div.appendChild(elmSvg);
ready

Revisions

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