3

MutationRecord.addedNodes returns NodeList with nodes detected as added in my document. When I use obj.appendChild() method, mutationObserver detects it and returns this ONE node in MutationRecord.addedNodes as nodelist [node]. When I use .appendChild() in loop, mutationObserver fires as many times as the number of loops amounts.

I'm just wondering if there is any case when mutationOvserver will detect more than one node added simultaneously and fire ONCE and return those nodes in MutationRecord.addedNodes as ONE nodelist [node,node,node,...]?

Paweł
  • 4,238
  • 4
  • 21
  • 40

1 Answers1

2

Judging by the spec one appendChild corresponds to a single-node addedNodes array.

addedNodes usually contains many nodes during the initial page load and, naturally, if many nodes were inserted by js code in one operation via insertAdjacentHTML, or document.write, or .innerHTML, or .outerHTML, or DocumentFragment with many nodes.

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • In other words there's no way to control the behavior of `mutationObserver` itself? (Assuming we are not allowed to refactor the portion of the code where the node insertions are done.) – Pacerier Feb 09 '17 at 20:30
  • Looking at the [step 5](https://www.w3.org/TR/dom/#queue-a-mutation-observer-compound-microtask) in the linked spec I presume you'd have to influence compound microtask scheduling to avoid coalescing. I have node idea as to how. – wOxxOm Feb 09 '17 at 20:40