Events vs Events2

JavaScript performance comparison

Revision 12 of this test case created

Preparation code

<script>
var GS = {};
GS.pubSub = function() {
  var subscribers = {},
    persistentEvents = {};

  this.on = function(ev, fn) {
    var events = ev.split(",");
    events.forEach(function(ev) {
      ev = jQuery.trim(ev);

      if(typeof subscribers[ev] === "undefined") {
        subscribers[ev] = [];
      }
      subscribers[ev].push(fn);

      if(persistentEvents.hasOwnProperty(ev)) {
        fn.apply(null, persistentEvents[ev]);
      }
    });
  };

  this.once = function(ev, fn) {
    var called = false,
      callback = function(arg) {
        arg = arg || [];
        if(!called) {
          fn.apply(null, arguments);
          called = true;
        }
      };

    GS.on(ev, callback);
  };

  this.fire = function(ev, arg, persist) {
    arg = arg || [];
    if(typeof subscribers[ev] !== "undefined") {
      subscribers[ev].forEach(function(current) {
        current.apply(null, arg);
      });
    }
    if(persist) {
      persistentEvents[ev] = arg;
    }
  };

  return this;
};


/* Set up global event queue, hanging pubsub functions on GS */
GS.pubSub.call( GS );
</script>

<script src='//cdnjs.cloudflare.com/ajax/libs/jquery/1.8.3/jquery.min.js'></script>
<script src='//underscorejs.org/underscore.js'></script>
<script src='//cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min.js'></script>
<script>var oldEvents = Backbone.Events;</script>
<script src='//cdnjs.cloudflare.com/ajax/libs/backbone.js/0.5.3/backbone-min.js'></script>
<script>var olderEvents = Backbone.Events;</script>
<script src='//backbonejs.org/backbone.js'></script>
<script>var masterEvents = Backbone.Events;</script>
<script>
Benchmark.prototype.setup = function() {
    old = _.extend({}, oldEvents);
    older = _.extend({}, olderEvents);
    master = _.extend({}, masterEvents);
    jquery = $({});
   
    numEvents = 3;
    numListeners = 100;
   
    _.times(numEvents, function(event) {
      _.times(numListeners, function() {
        older.bind("event" + event, function(){});
        old.on("event" + event, function(){});
        jquery.on("event" + event, function(){});
        master.on("event" + event, function(){});
        GS.on("event" + event, function() {});
      });
    });
   
};
</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 1.8.3
for (var i = 0; i < numEvents; i++) {
  jquery.trigger('event' + i, 1, 2);
}
pending…
Backbone 0.5.3
for (var i = 0; i < numEvents; i++) {
  older.trigger('event' + i, 1, 2);
}
pending…
Backbone 0.9.2
for (var i = 0; i < numEvents; i++) {
  old.trigger('event' + i, 1, 2);
}
pending…
Backbone 0.9.9
for (var i = 0; i < numEvents; i++) {
  master.trigger('event' + i, 1, 2);
}
pending…
GS
for (var i = 0; i < numEvents; i++) {
  GS.fire('event' + i, [1, 2]);
}
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