pub-sub-vs-pub-sub

JavaScript performance comparison

Revision 2 of this test case created by Keegan

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<div id="out"></div>
<script>
Benchmark.prototype.setup = function() {
    (function($) {
        var o = $({});
        $.a_subscribe = function() {
        o.bind.apply(o, arguments);
        };
        $.a_unsubscribe = function() {
        o.unbind.apply(o, arguments);
        };
        $.a_publish = function() {
        o.trigger.apply(o, arguments);
        };
    }(jQuery));
    (function(d){
        var cache = {};
        d.b_publish = function(topic,args){
            cache[topic] && d.each(cache[topic], function(){
                this.apply(d, args || []);
            });
        };
   
        d.b_subscribe = function(topic, callback){    
            if(!cache[topic]){
                cache[topic] = [];
            }
            cache[topic].push(callback);
            return [topic, callback]; // Array
        };
   
        d.b_unsubscribe = function(handle){    
            // handle: Array - The return value from a $.subscribe call.
            var t = handle[0];
            cache[t] && d.each(cache[t], function(idx){
                if(this == handle[1]){
                    cache[t].splice(idx, 1);
                }
            });
        };
    })(jQuery);
   
    function handle(a, b) {
        $("#out").append(a +" "+ b);
    };
};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
tiny-pub-sub
$.a_subscribe("/some/topic", handle);
$.a_publish("/some/topic", [ "a", "b", "c" ]);
$.a_unsubscribe("/some/topic", handle);
pending…
jquery-pub-sub-plug
var hdn = $.b_subscribe("/some/topic", handle);
$.b_publish("/some/topic", ["a","b","c"]);
$.b_unsubscribe("/some/topic", hdn);
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