A detached tree (or disconnected tree) is a DOM tree that hasn't been added to the document yet. Mutating DOM nodes that are not currently appended to a document is extremely cheap.
Example:
Slow Version
Items are appended to the list, which is already "connected" because it has been inserted into document.body:
Fast Version
All we have to change here is to move the document.body.appendChild(list) to the end.
Because items are appended to the list before the list is appended to document.body, building up each item and inserting it into the list is very cheap. Rendering can only happen after the very last line of code executes.
letlist=document.createElement('ul');for(leti=0;i<1000;i++){letitem=document.createElement('li');item.textContent=`Item ${i}`;list.append(item);}document.body.appendChild(list);// we just moved this to the end
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
A detached tree (or disconnected tree) is a DOM tree that hasn't been added to the document yet. Mutating DOM nodes that are not currently appended to a document is extremely cheap.
Example:
Slow Version
Items are appended to the list, which is already "connected" because it has been inserted into
document.body
:Fast Version
All we have to change here is to move the
document.body.appendChild(list)
to the end.Because items are appended to the list before the list is appended to
document.body
, building up each item and inserting it into the list is very cheap. Rendering can only happen after the very last line of code executes.