Hi.
I'm wondering what is the right way to do dom insertion of complex elements.
Until now, (with jQuery) I used to first build my element and to insert it once finished. I thought that it was the most efficient, since DOM access is costly performance-wise.
But with native js, I read that to avoid memory leaks, each new dom node should be inserted in the dom right after its creation.
Our intranet says :
Dom insertion order Pay attention to DOM insertion order: never append a child element to a root element before the root is itself appended to the DOM.
var root = document.createElement("DIV");
var child = document.createElement("DIV");
// THIS IS WRONG
root.appendChild(child);
document.body.appendChild(root);
// THIS IS CORRECT
document.body.appendChild(root);
root.appendChild(child);
I found online this page which basically explain the same thing (under the Cross-Page Leaks section) : http://www.codeweblog.com/javascript-memory-leak-on-the-collation-of-draft-issues/
Does that mean that there is an opposition between performance and leak-prevention ?
Should new DOM elements be created and manipulated as string before to be inserted ? How are js libraries solving this ? Is DocumentFragments the miracle solution ?