I'm curious whether or not the batch of mutations that are received by a MutationObserver is returned in order or not.
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
console.log(mutation.type); // are these in order?
});
});
Thanks!
No. They are not guaranteed to be in order, which makes things really difficult.
Take a look at this example, for example:
https://codepen.io/trusktr/pen/OJveVbv/d7f4cc48f8fa3b7462ae8043157ba05d
Notice in the console output that all records for one parent element are looped over first, then all records for the other parent are looped over.
This is entirely different than the code that actually ran, in which it alternated between parent one and parent two.
On a side note, Mutation Observer is very difficult to use compared to DOM Mutation Events. I strongly believe that a better intuitive non-performance-killing version of an API similar to Mutation Events could have been created instead of MutationObserver's childList API.
More details on the complexity of MutationObserver vs the simplicity of Mutation Events regarding the developer experience here:
https://github.com/whatwg/dom/issues/1105
Please express the need for a simpler API there if you agree!