PubSubJS vs. jQuery custom events (v3)

Revision 3 of this benchmark created by Morgan Roderick on


Description

An attempt at showing that PubSubJS is faster than using jQuery custom evens for publish/subscribe style messaging.

It's certainly not as rich in features, and I am happy with that.

Introducing PubSubJS

2010-11-28: Updated to PubSubJS v0.1, which has separate method for syncronous publishing.

Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="//github.com/mroderick/PubSubJS/raw/master/pubsub.min.js"></script>
<script>
  var callback1 = function() {};
  var callback2 = function() {};
  var payload = {
   somekey: 'some value'
  };
  var body;
  
  // let's use jQuery.ready to make sure that the DOM is ready,
  // before trying to work with it
  jQuery(function() {
   // we'll use the body element to exchange messages for jQuery
   // if using deeper nested elements, jQuery will be slower, as custom events bubble
   body = $('body');
  
   // subscribe our callback1 function to the custom event for jQuery, only once
   body.bind('my-event', callback1);
  
   // subscribe our callback2 function to the message for PubSub
   PubSub.subscribe('my-event', callback2)
  });
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
1. jQuery - trigger
body.trigger('my-event', payload);
ready
2. PubSub - publish - asyncronous
PubSub.publish('my-event', payload);
ready
3. PubSub - publish - syncronous
PubSub.publishSync('my-event', payload);
ready

Revisions

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