Pub/Sub implementations

Benchmark created by Ralph Holzmann on


Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
  /*    
  
        jQuery pub/sub plugin by Peter Higgins (dante@dojotoolkit.org)
  
        Loosely based on Dojo publish/subscribe API, limited in scope. Rewritten blindly.
  
        Original is (c) Dojo Foundation 2004-2010. Released under either AFL or new BSD, see:
        http://dojofoundation.org/license for more information.
  
  */
  
  ;
  (function(d) {
  
   // the topic/subscription hash
   var cache = {};
  
   d.publish1 = function( /* String */ topic, /* Array? */ args) {
    // summary: 
    //          Publish some data on a named topic.
    // topic: String
    //          The channel to publish on
    // args: Array?
    //          The data to publish. Each array item is converted into an ordered
    //          arguments on the subscribed functions. 
    //
    // example:
    //          Publish stuff on '/some/topic'. Anything subscribed will be called
    //          with a function signature like: function(a,b,c){ ... }
    //
    //  |               $.publish("/some/topic", ["a","b","c"]);
    cache[topic] && d.each(cache[topic], function() {
     this.apply(d, args || []);
    });
   };
  
   d.subscribe1 = function( /* String */ topic, /* Function */ callback) {
    // summary:
    //          Register a callback on a named topic.
    // topic: String
    //          The channel to subscribe to
    // callback: Function
    //          The handler event. Anytime something is $.publish'ed on a 
    //          subscribed channel, the callback will be called with the
    //          published array as ordered arguments.
    //
    // returns: Array
    //          A handle which can be used to unsubscribe this particular subscription.
    //  
    // example:
    //  |       $.subscribe("/some/topic", function(a, b, c){ /* handle data */ });
    //
    if (!cache[topic]) {
     cache[topic] = [];
    }
    cache[topic].push(callback);
    return [topic, callback]; // Array
   };
  
   d.unsubscribe1 = function( /* Array */ handle) {
    // summary:
    //          Disconnect a subscribed function for a topic.
    // handle: Array
    //          The return value from a $.subscribe call.
    // example:
    //  |       var handle = $.subscribe("/something", function(){});
    //  |       $.unsubscribe(handle);
    var t = handle[0];
    cache[t] && d.each(cache[t], function(idx) {
     if (this == handle[1]) {
      cache[t].splice(idx, 1);
     }
    });
   };
  
  })(jQuery);
  
  
  
  /*!
   * jQuery Tiny Pub/Sub - v0.3pre - 11/4/2010
   * http://benalman.com/
   * 
   * Copyright (c) 2010 "Cowboy" Ben Alman
   * Dual licensed under the MIT and GPL licenses.
   * http://benalman.com/about/license/
   */
  
  (function($) {
  
   var o = $({});
  
   $.subscribe2 = function() {
    o.bind.apply(o, arguments);
   };
  
   $.unsubscribe2 = function() {
    o.unbind.apply(o, arguments);
   };
  
   $.publish2 = function() {
    o.trigger.apply(o, arguments);
   };
  
  })(jQuery);
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Object cache
$.subscribe1('foo', function() {
 1 + 1;
});

$.publish1('foo', ['bar']);

$.unsubscribe1('foo');
ready
jQuery events
$.subscribe2('foo', function() {
 1 + 1;
});

$.publish2('foo', ['bar']);

$.unsubscribe2('foo');
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.