MutationObserver.vs.MessageChannel.vs.settimeout

JavaScript performance comparison

Revision 3 of this test case created by treelite

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    fn = function() {
      deferred.resolve();
    };
    var channel = new MessageChannel();
    // linked list of tasks (single, with head node)
    var head = {},
        tail = head;
    channel.port1.onmessage = function() {
      head = head.next;
      var task = head.task;
      delete head.task;
      task();
    };
    nextTick_messageChannel = function(task) {
      tail = tail.next = {
        task: task
      };
      channel.port2.postMessage(0);
    };
   
    nextTick_setTimeout = function(task) {
      setTimeout(task, 0);
    }
   
    var nextTick_MutationObserver = (function () {
   
        var callbacks = [];
        var attributeName = 'x';
   
        var Observer = window.MutationObserver || window.webKitMutationObserver;
   
        var observer = new Observer(function (mutations) {
            var item = mutations[0];
            if (item.attributeName == attributeName) {
                var len = callbacks.length;
                for (var i = 0; i < len; i++) {
                    callbacks[i]();
                }
                callbacks.splice(0, i);
            }
        });
   
        var ele = document.createElement('div');
       
        observer.observe(ele, {attributes: true});
   
        return function (fn) {
            callbacks.push(fn);
            ele.setAttribute(attributeName, Date.now());
        };
   
    })();
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
setTimeout
// async test
nextTick_setTimeout(fn);
pending…
MessageChannel
// async test
nextTick_messageChannel(fn);
pending…
MutationObserver
// async test
nextTick_MutationObserver(fn);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment