What you are doing is probably great for the vast majority of use cases. Your code will ultimately call the DOM function appendChild once for each li , and I'm sure that all answers will still be doing. And again, this is probably good, although it can cause melting every time.
In those situations where you absolutely need to not have to duplicate the DOM on each pass through the loop, you probably need a piece of the document .
var i, li, frag = document.createDocumentFragment(); for(i=0; i<=5; i++){ li = $("<li>" + "teest "+i + "</li>"); frag.appendChild(li[0]); } $("#ulid").append(frag);
When you add a fragment, the children of the fragment are added, not the actual fragment. The advantage is that, being the only DOM call, you give the DOM engine the ability to add all the elements and only pay at the end.
source share