Repository: facebook/react
File: packages/react-dom-bindings/src/server/ReactDOMServerExternalRuntime.js (JavaScript)

Code before:
  handleExistingNodes((document.body /*: HTMLElement */));
} else {
  // Document must be loading -- body may not exist yet if the fizz external
  // runtime is sent in <head> (e.g. as a preinit resource)
  // $FlowFixMe[recursive-definition]
  const domBodyObserver = new MutationObserver(() => {
    // We expect the body node to be stable once parsed / created
    if (document.body != null) {
      if (document.readyState === 'loading') {
        installFizzInstrObserver(document.body);
      }
      // $FlowFixMe[incompatible-cast]
      handleExistingNodes((document.body /*: HTMLElement */));

      // We can call disconnect without takeRecord here,
      // since we only expect a single document.body
      domBodyObserver.disconnect();
    }
  });
  // documentElement must already exist at this point
  // $FlowFixMe[incompatible-call]
  domBodyObserver.observe(document.documentElement, {childList: true});
}

function handleExistingNodes(target /*: HTMLElement */) {
  const existingNodes = Array.from(target.children);
  for (let i = 0; i < existingNodes.length; i++) {
    handleNode(existingNodes[i]);
  }
}

function installFizzInstrObserver(target /*: Node */) {
  const handleMutations = (mutations /*: Array<MutationRecord> */) => {
    for (let i = 0; i < mutations.length; i++) {
      const addedNodes = mutations[i].addedNodes;
      for (let j = 0; j < addedNodes.length; j++) {
        if (addedNodes.item(j).parentNode) {
          handleNode(addedNodes.item(j));
        }
      }
    }
  };

  const fizzInstrObserver = new MutationObserver(handleMutations);
  // We assume that instruction data nodes are eventually appended to the
  // body, even if Fizz is streaming to a shell / subtree.
  fizzInstrObserver.observe(target, {
    childList: true,
  });
  window.addEventListener('DOMContentLoaded', () => {
    handleMutations(fizzInstrObserver.takeRecords());

Review comment (suggestion):
I'm pretty sure `querySelectorAll` will outperform this by a lot. In practice it may not make a huge difference given the (un)popularity of template in many sites

Code after:
  handleExistingNodes((document.body /*: HTMLElement */));
} else {
  // Document must be loading -- body may not exist yet if the fizz external
  // runtime is sent in <head> (e.g. as a preinit resource)
  // $FlowFixMe[recursive-definition]
  const domBodyObserver = new MutationObserver(() => {
    // We expect the body node to be stable once parsed / created
    if (document.body != null) {
      if (document.readyState === 'loading') {
        installFizzInstrObserver(document.body);
      }
      // $FlowFixMe[incompatible-cast]
      handleExistingNodes((document.body /*: HTMLElement */));

      // We can call disconnect without takeRecord here,
      // since we only expect a single document.body
      domBodyObserver.disconnect();
    }
  });
  // documentElement must already exist at this point
  // $FlowFixMe[incompatible-call]
  domBodyObserver.observe(document.documentElement, {childList: true});
}

function handleExistingNodes(target /*: HTMLElement */) {
  const existingNodes = target.querySelectorAll('template');
  for (let i = 0; i < existingNodes.length; i++) {
    handleNode(existingNodes[i]);
  }
}

function installFizzInstrObserver(target /*: Node */) {
  const handleMutations = (mutations /*: Array<MutationRecord> */) => {
    for (let i = 0; i < mutations.length; i++) {
      const addedNodes = mutations[i].addedNodes;
      for (let j = 0; j < addedNodes.length; j++) {
        if (addedNodes[j].parentNode) {
          handleNode(addedNodes[j]);
        }
      }
    }
  };

  const fizzInstrObserver = new MutationObserver(handleMutations);
  // We assume that instruction data nodes are eventually appended to the
  // body, even if Fizz is streaming to a shell / subtree.
  fizzInstrObserver.observe(target, {
    childList: true,
  });
  window.addEventListener('DOMContentLoaded', () => {
    handleMutations(fizzInstrObserver.takeRecords());