postMessage vs setTimeout

JavaScript performance comparison

Test case created

Preparation code

<script type="text/javascript" src="http://yui.yahooapis.com/combo?3.7.2/build/yui-base/yui-base-min.js&3.7.2/build/oop/oop-min.js"></script>
<script>
Benchmark.prototype.setup = function() {
    var soonPostMessage = function (callbackFunction) {
            var canceled;
   
            soonPostMessage._asynchronizer(function () {
                if (!canceled) {
                    callbackFunction();
                }
            });
   
            return {
                cancel: function () {
                    canceled = 1;
                }
            };
        },
        soonSetTimeout = function (callbackFunction) {
            var canceled;
   
            soonSetTimeout._asynchronizer(function () {
                if (!canceled) {
                    callbackFunction();
                }
            });
   
            return {
                cancel: function () {
                    canceled = 1;
                }
            };
        },
        subscribedToMessageEvent,
        Y = YUI().use('oop');
   
    soonPostMessage._asynchronizer = function (callbackFunction) {
        if (!subscribedToMessageEvent) {
            YUI.Env.add(Y.config.win, 'message', function (event) {
                if (event.source === Y.config.win) {
                    var id = event.data,
                        originId = id + 'origin',
   
                        callbackFunction = soonPostMessage._asynchronizer[id];
   
                    if (callbackFunction && event.origin === soonPostMessage._asynchronizer[originId]) {
                        callbackFunction();
   
                        delete soonPostMessage._asynchronizer[id];
                        delete soonPostMessage._asynchronizer[originId];
   
                        if (event.stopImmediatePropagation) {
                            event.stopImmediatePropagation();
                        } else {
                            event.stopPropagation();
                        }
   
                        event.preventDefault();
                    }
                }
            }, false);
   
            subscribedToMessageEvent = true;
        }
   
        var id = Y.guid('soon'),
   
            location = Y.getLocation(),
            origin = location.origin || (location.protocol + '//' + location.host);
   
        soonPostMessage._asynchronizer[id] = callbackFunction;
        soonPostMessage._asynchronizer[id + 'origin'] = origin;
   
        Y.config.win.postMessage(id, origin);
    };
    soonSetTimeout._asynchronizer = function (callbackFunction) {
        setTimeout(callbackFunction, 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
postMessage
// async test
soonPostMessage(function () {
    deferred.resolve();
});
pending…
setTimeout
// async test
soonSetTimeout(function () {
    deferred.resolve();
});
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