Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

DocumentFragment — grouping DOM nodes

DocumentFragment groups DOM nodes without a wrapper element. Inserting the fragment moves its children and leaves it empty.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

const fragment = new DocumentFragment();
// Alternative:
const otherFragment = document.createDocumentFragment();
fragment.append(node);
parent.appendChild(fragment);

DocumentFragment is a DOM node, not an element. Create it using new DocumentFragment() or document.createDocumentFragment() to prepare children outside the document tree.

parent.appendChild(fragment) moves children in order, without cloning or a wrapper. The fragment becomes empty and remains reusable. The example also checks node identity.

This is a Web API, not an ECMAScript feature. The example requires a browser document; Node 24 does not prove support. Do not assume a guaranteed speedup.

Examples

(() => {
  const fragment = new DocumentFragment();
  const list = document.createElement('ul');
  const first = document.createElement('li');
  first.textContent = 'A';
  fragment.append(first);
  const second = document.createElement('li');
  second.textContent = 'B';
  fragment.append(second);

  list.appendChild(fragment);
  console.log(list.textContent);
  console.log(list.children.length);
  console.log(fragment.childNodes.length);
  console.log(list.firstChild === first);
})();

Result

AB
2
0
true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources