createDocumentFragment vs appendChild on non-appended element

JavaScript performance comparison

Revision 17 of this test case created

Info

Justice

Preparation code

<form id="fform"></form>
<script>
window.fform = document.getElementById('fform')
</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
Normal Append

var elems = [
  document.createElement("hr"),
  text( document.createElement("b"), "Links:" ),
  document.createTextNode(" "),
  text( document.createElement("a"), "Link A" ),
  document.createTextNode(" | "),
  text( document.createElement("a"), "Link B" ),
  document.createTextNode(" | "),
  text( document.createElement("a"), "Link C" )
];

function text(node, txt){
  node.appendChild( document.createTextNode(txt) );
  return node;
}


var div = document.getElementsByTagName("div");

for ( var i = 0; i < div.length; i++ ) {
  for ( var e = 0; e < elems.length; e++ ) {
    div[i].appendChild( elems[e].cloneNode(true) );
  }
}

 
pending…
Document Fragment+Form

var elems = [
  document.createElement("hr"),
  text( document.createElement("b"), "Links:" ),
  document.createTextNode(" "),
  text( document.createElement("a"), "Link A" ),
  document.createTextNode(" | "),
  text( document.createElement("a"), "Link B" ),
  document.createTextNode(" | "),
  text( document.createElement("a"), "Link C" )
];

function text(node, txt){
  node.appendChild( document.createTextNode(txt) );
  return node;
}


var div = document.getElementsByTagName("div");

var fragment = document.createDocumentFragment();
for ( var e = 0; e < elems.length; e++ ) {
  fragment.appendChild( elems[e] );
}

for ( var i = 0; i < div.length; i++ ) {
  div[i].appendChild( fragment.cloneNode(true) );
}

 
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