pub-sub-vs-pub-sub

JavaScript performance comparison

Test case created by Keegan

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></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
tiny-pub-sub
if ( !window.init ) { 
window.init = (function($) {

        var o = $({});

        $.subscribe = function() {
        o.bind.apply(o, arguments);
        };

        $.unsubscribe = function() {
        o.unbind.apply(o, arguments);
        };

        $.publish = function() {
        o.trigger.apply(o, arguments);
        };
  return true;
}(jQuery));
}

function handle(e, a, b, c) {
        console.log(a + b + c);
};

$.subscribe("/some/topic", handle);
$.publish("/some/topic", [ "a", "b", "c" ]);
$.unsubscribe("/some/topic", handle);
 
pending…
jquery-pub-sub-plug
if ( !window.init ) { 
window.init = (function(d){
        var cache = {};
        d.publish = function(/* String */topic, /* Array? */args){
            cache[topic] && d.each(cache[topic], function(){
                this.apply(d, args || []);
            });
        };

        d.subscribe = function(/* String */topic, /* Function */callback){    
            if(!cache[topic]){
                cache[topic] = [];
            }
            cache[topic].push(callback);
            return [topic, callback]; // Array
        };

        d.unsubscribe = function(/* Array */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);
                }
            });
        };
  return true;
})(jQuery);
}

function handle(a, b, c) {
        console.log(a + b + c);
};

var hdn = $.subscribe("/some/topic", handle);
$.publish("/some/topic", ["a","b","c"]);
$.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