setImmediate Test

JavaScript performance comparison

Revision 6 of this test case created

Info

set immediate test

Preparation code

<script>
    setTimeout(function () {
        'use strict';

        if (!window.setImmediate) {
            ui.benchmarks[0].options.defer = false;
        }

        if (!window.msSetImmediate) {
            ui.benchmarks[2].options.defer = false;
        }

        if (!window.MessageChannel) {
            ui.benchmarks[3].options.defer = false;
        } else {
            window.immediateCallbacks = {};
            window.immediateChannel = new MessageChannel();

            immediateChannel.port1.onmessage = function (event) {
                immediateCallbacks[event.data]();
                delete immediateCallbacks[event.data];
            };
        }

        if ("onreadystatechange" in document.createElement("script")) {
            window.readyStateChange = true;
        } else {
            ui.benchmarks[4].options.defer = false;
        }

        if (!window.postMessage) {
            ui.benchmarks[5].options.defer = false;
        } else {
            window.immediateCallbacks = {};

            var onGlobalMessage = function (event) {
                if (event.source === window && event.data.indexOf('immediate') === 0) {
                    var handle = 'h' + event.data.substring(9);
                    immediateCallbacks[handle]();
                    delete immediateCallbacks[handle];
                }
            };

            if (window.addEventListener) {
                window.addEventListener("message", onGlobalMessage, false);
            } else {
                window.attachEvent("onmessage", onGlobalMessage);
            }
        }

        if (window.MutationObserver || window.WebKitMutationObserver) {
            window.element = document.createElement('div');
            window.immediateCallbacks = [];

            new (window.MutationObserver || window.WebKitMutationObserver)(function () {
                immediateCallbacks.forEach(function(callbackFunction) {
                    callbackFunction();
                });

                window.immediateCallbacks = [];
            }).observe(element, {
                attributes: true
            });
        } else {
            ui.benchmarks[6].options.defer = false;
        }
    }, 0);
</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
setImmediate
// async test
setImmediate(function () {
    deferred.resolve();
});
pending…
setTimeout
// async test
setTimeout(function () {
    deferred.resolve();
}, 0);
pending…
msSetImmediate
// async test
msSetImmediate(function () {
    deferred.resolve();
});
pending…
MessageChannel
// async test
var handle = 'h' + Math.random()

immediateCallbacks[handle] = function () {
    deferred.thisWillFailSynchronously();
};

immediateChannel.port2.postMessage(handle);

immediateCallbacks[handle] = function () {
    deferred.resolve();
};
pending…
readyStateChange
// async test
var scriptEl = document.createElement("script");
scriptEl.onreadystatechange = function () {
    scriptEl.onreadystatechange = null;
    scriptEl.parentNode.removeChild(scriptEl);
    scriptEl = null;

    deferred.resolve();
};

document.documentElement.appendChild(scriptEl);
readyStateChange;
pending…
postMessage
// async test
var handle = Math.random()

immediateCallbacks['h' + handle] = function () {
    deferred.thisWillFailSynchronously();
};

postMessage('immediate' + handle, "*");

immediateCallbacks['h' + handle] = function () {
    deferred.resolve();
};
pending…
MutationObserver
// async test
immediateCallbacks.push(function () {
    deferred.resolve();
});
element.setAttribute('drainQueue', 'drainQueue');
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