A Comparison of JS Publish/Subscribe Approaches

JavaScript performance comparison

Revision 21 of this test case created

Info

A Comparison of JS Publish/Subscribe Approaches

More info: publish/subscribe on Wikipedia.

Compared:

Preparation code

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script src="https://raw.github.com/mroderick/PubSubJS/master/src/pubsub.js">
</script>
<script src="https://raw.github.com/phiggins42/bloody-jquery-plugins/master/pubsub.js">
</script>
<script src="https://raw.github.com/phiggins42/bloody-jquery-plugins/55e41df9bf08f42378bb08b93efcb28555b61aeb/pubsub.js">
</script>
<script src="https://raw.github.com/gist/1695338/736bcf94f9226d975fce3ad4f2bc08ccbf176bad/pubsub.js">
</script>
<script src="https://raw.github.com/appendto/amplify/master/core/amplify.core.js">
</script>
<script src="https://raw.github.com/maccman/spine/master/lib/spine.js">
</script>
 
<script>
Benchmark.prototype.setup = function() {
    var callback = function() { };
    var payload = {
      somekey: 'some value'
    };
    var body;
    var handle;
    var topics={};
   
      $(window).bind('my-event', callback);
      PubSub.subscribe('my-event', callback)
      $.subscribe('my-event', callback);
      handle = Events.subscribe('my-event', callback);
      App.subscribe('my-event', callback);
      amplify.subscribe('my-event', callback);
      Spine.bind('my-event', callback);
      topics["my-event"] = $.Callbacks();
      topics["my-event"].add(callback);
   
};
</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
jQuery Events
// async test
$(window).bind('my-event', function(){
  deferred.resolve();
});
$(window).trigger('my-event');
$(window).unbind('my-event');
 
pending…
PubSubJS
PubSub.publish('my-event', payload);
pending…
jQuery PubSub plugin
$.publish('my-event', [payload]);
pending…
Pure JS PubSub
Events.publish('my-event', [payload]);
pending…
Darcy Clarke
App.publish('my-event', [payload]);
pending…
Amplify Pub/Sub
amplify.publish('my-event', payload);
pending…
Spine Events
Spine.trigger('my-event', payload);
pending…
jQuery Callbacks
topics["my-event"].fire(payload);
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