DOM MutationObserver vs. CSS Animation vs. Mutation Events (v6)

Revision 6 of this benchmark created on


Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<p id="message"></p>
<div id="haystack"></div>

<style type="text/css">
@-moz-keyframes nodeInserted {
  from { clip: rect(1px, auto, auto, auto); }
  to { clip: rect(0px, auto, auto, auto); }
}
@-webkit-keyframes nodeInserted {
  from { clip: rect(1px, auto, auto, auto); }
  to { clip: rect(0px, auto, auto, auto); }
}
@-ms-keyframes nodeInserted {
  from { clip: rect(1px, auto, auto, auto); }
  to { clip: rect(0px, auto, auto, auto); }
}
@-o-keyframes nodeInserted {
  from { clip: rect(1px, auto, auto, auto); }
  to { clip: rect(0px, auto, auto, auto); }
}
@keyframes nodeInserted {
  from { clip: rect(1px, auto, auto, auto); }
  to { clip: rect(0px, auto, auto, auto); }
}

#haystack > span.needle {
  -o-animation-duration: 0.001s;
  -ms-animation-duration: 0.001s;
  -moz-animation-duration: 0.001s;
  -webkit-animation-duration: 0.001s;
  animation-duration: 0.001s;
  -o-animation-name: nodeInserted;
  -ms-animation-name: nodeInserted;
  -moz-animation-name: nodeInserted;
  -webkit-animation-name: nodeInserted;
  animation-name: nodeInserted;
}
</style>

<script>
console.log("preparation");
var globalDeferred;
var haystack = document.getElementById("haystack");

// Note: child elements must be created each time they're appended
var addHayTo = function(haystack, count) {
  for (var i = 0; i < count; i++) {
    var hay = document.createElement("span");
    hay.className = "hay";
    haystack.appendChild(hay);
  }
};

var addNeedleTo = function(haystack) {
  var needle = document.createElement("span");
  needle.className = "needle";
  haystack.appendChild(needle);
};

var isNeedle = function(node) {
  return node.nodeName.toUpperCase() === "SPAN" &&
    node.classList && node.classList.contains("needle");
};

var handleNeedleFound = function() {
  globalDeferred.resolve();
  globalDeferred = null;
};

var setMessage = function(messageHtml) {
  document.getElementById("message").innerHTML = messageHtml;
};

var mutationListener = function(mutations) {
  mutations.forEach(function(mutation) {
    if (mutation.type === "childList" && mutation.addedNodes !== null) {
      for (var i = 0; i < mutation.addedNodes.length; i++) {
        if (isNeedle(mutation.addedNodes[i])) handleNeedleFound();
      }
    }
  });
};

var observer;
if (window.MutationObserver) {
  setMessage("Your browser supports <code>MutationObserver</code>.");
  observer = new MutationObserver(mutationListener);
} else if (window.WebKitMutationObserver) {
  setMessage("Your browser supports <code>WebKitMutationObserver</code>.");
  observer = new WebKitMutationObserver(mutationListener);
} else {
  setMessage("Your browser does not support <code>MutationObserver</code>. You will not be able to run these tests.");
}

var animationListener = function(event) {
  if (event.animationName === "nodeInserted" && isNeedle(event.target))
    handleNeedleFound();
};

var nodeInsertedListener = function(event) {
  if (isNeedle(event.target))
    handleNeedleFound();
};

// Note: cannot use global setup when using ui.benchmarks[i].setup
ui.benchmarks[0].setup = function() {
  console.log("setup[0]");
  observer.observe(haystack, {
    childList: true,
    subtree: true
  });
};

ui.benchmarks[1].setup = function() {
  console.log("setup[1]");
  haystack.addEventListener("animationstart", animationListener, false);
  haystack.addEventListener("MSAnimationStart", animationListener, false);
  haystack.addEventListener("webkitAnimationStart", animationListener, false);
};

ui.benchmarks[2].setup = function() {
  console.log("setup[2]");
  haystack.addEventListener("DOMNodeInserted",
    nodeInsertedListener, false);
};

ui.benchmarks[0].teardown = function() {
  console.log("teardown[0]");
  observer.disconnect();
  haystack.innerHTML = "";
};

ui.benchmarks[1].teardown = function() {
  console.log("teardown[1]");
  haystack.removeEventListener("animationstart", animationListener, false);
  haystack.removeEventListener("MSAnimationStart", animationListener, false);
  haystack.removeEventListener("webkitAnimationStart",
    animationListener, false);
  haystack.innerHTML = "";
};

ui.benchmarks[2].teardown = function() {
  console.log("teardown[2]");
  haystack.removeEventListener("DOMNodeInserted",
    nodeInsertedListener, false);
  haystack.innerHTML = "";
};
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
MutationObserver
// async test
globalDeferred = new $.Deferred;
addHayTo(haystack, 100);
addNeedleTo(haystack);
ready
CSS Animations
// async test
globalDeferred = new $.Deferred;
addHayTo(haystack, 100);
addNeedleTo(haystack);
ready
Mutation Events (DOMNodeInserted)
// async test
globalDeferred = new $.Deferred;
addHayTo(haystack, 100);
addNeedleTo(haystack);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.