Modifying the document tree

It is important to know that making modifications while traversing the tree is very expensive. It is best to create a temporary collection to work on rather than modifying the tree directly while looping over all of its nodes.

Indeed, the best approach is to use a non-displayed DOM tree fragment, to make all the changes at once and then, to display them all together. Here is a theoretical example of how this can be accomplished:

function myJS(){    let docFragment = document.createDocumentFragment();    let element, content;        for(let i = 0; i < list.length; i++) {        element = document.createElement("p");        content = document.createTextNode(list[i]);        element.appendChild(content);        docFragment.appendChild(element);    }     document.body.appendChild(docFragment); ...

Get Mastering The Faster Web with PHP, MySQL, and JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.