As far as I know, the best advantage is adding a large number of isolated elements to the fragment and adding this element before all children and attributes are fixed (post append). If I understand your code (I have to decode it lazily), there is one gap that you add to the fragment. This is not the meaning of documentFragment. By the way: you should not declare your vars in a loop.
var node=document.getElementById("whatever") ,frag=document.createDocumentFragment() ,i=0,len=50,a={},img={}; for(i;i<len;i++){ a=document.createElement("a"); img=document.createElement("img"); a.href="image"+i; img.className=J[i][1]; img.src="image/img"+i+".png"; img.alt="image:"+i; a.appendChild(img); frag.appendChild(a); } node.appendChild(frag);
Thus, IE8 Opera12 takes the same time as innerHTML. The real benefit is chrome. FF is incredibly fast with innerHTML. Tested on an old XP machine.
Another thought is to create a node that is not associated with the DOM with all the child elements and attributes, clone it several times, manipulate it and add it to documentFragment.
var frag=document.createDocumentFragment() ,toFill=document.getElementById("imageCollection") ,i=0,a={},img={} ,dummy=document.createElement("a") ; dummy.innerHTML="<img src='img/image_' />"; for(i;i<50;i++){ a=dummy.cloneNode(true); img=a.getElementsByTagName("img")[0]; a.href="description_"+i+".html"; img.src+=i+".png"; frag.appendChild(a); } toFill.appendChild(frag);
This is useful if you do not need to do a lot of manipulation of the cloned node.
source share