pubsub.js vs jQuery events

JavaScript performance comparison

Revision 7 of this test case created by Jack

Info

pubsub.js tend to be the richest in functionality publish/subscribe JavaScript library.

pubsub.js library

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="https://raw.github.com/Sahadar/pubsub.js/master/pubsub.js"></script>
<script src="https://gist.github.com/datibbaw/5582687/raw/e1f136c174f4816294e7f4cc692d922dada65cfc/pubsub.js"></script>
<script>
  var callback1 = function(event) {
    return false;
  };
  var callback2 = function() {};
  var payload = {
   somekey: 'some value'
  };
  var body;
  var someJqueryObject = $({});
  var pb = new datibbaw.PubSub();
 
  // 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 callback1 function to the custom event for the plain jQuery object (non-DOM)
   someJqueryObject.bind('my-event', callback1);
 
   // subscribe our callback2 function to the message for PubSub
   pubsub.subscribe('my-event', callback2)
pb.on('my-event', callback2);
 
   // Use document instead of 'body' as the anchor for custom events
   doc = $(document)
  });
</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 DOM - trigger
body.trigger('my-event', payload);
pending…
jQuery Object - trigger
someJqueryObject.trigger('my-event', payload);
pending…
pubsub.js - publish
pubsub.publish('my-event', payload);
pending…
jQuery document - trigger
doc.trigger('my-event', payload);
pending…
datibbaw - pubsub
pb.trigger('my-event', 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:

1 comment

Wojciech Dłubacz commented :

somebody has not read pubsub.js documentation (publish's 2nd argument)...

Add a comment