When an update needs several nodes, a DOM fragment provides a lightweight container for assembling them outside the page’s live tree. Add the finished nodes to the page together instead of inserting each one as it is created. section 6.2.9
Build a list before attaching it
Create a fragment with document.createDocumentFragment(). For each item, create an li with document.createElement('li'), set its textContent, and append it to the fragment. Finally, call list.append(fragment). The list receives the children; the fragment does not become a wrapper around them. section 6.2.9 section 8.1.2
An HTML template uses a fragment differently: template.content holds parsed markup that is not rendered or run while it remains in the template. This makes a template useful for reusable rows, cards, and component markup without a hidden wrapper or an HTML string assembled in JavaScript. A fragment used as a construction buffer is not, by itself, a reusable blueprint. section 8.1 section 8.1.2
Keep the fragment’s ownership and one-time insertion behavior in mind:
- Appending a fragment moves its children into the destination and leaves the fragment empty. For repeated UI, insert a deep copy from template.content.cloneNode(true), or use document.importNode(template.content, true). section 8.1.2
- Attach event listeners to the inserted copies, not to template.content. Cloning creates new nodes and does not copy listeners registered with addEventListener. section 8.1.2
- For nodes originating in another document, account for document ownership: adoptNode transfers a node, while importNode clones it into the destination document. section 6.2.3


