coffee vs js events

JavaScript performance comparison

Test case created by Joshua Kehn

Preparation code

<script type="text/javascript">
function EventBinder () {
    this.events = {};
};

EventBinder.prototype.bind = function bind (on, fn, context) {
    if (!this.events[on]) {
        this.events[on] = [];
    }
    context = context || this;
    this.events[on].push({handler : fn, context : context});
};

EventBinder.prototype.trigger = function trigger (evt) {
    var args = Array.prototype.slice.call(arguments).slice(1);

    if (this.events[evt]) {
        this.events[evt].forEach(function (caller) {
            caller.handler.apply(caller.context, args);
        });
    }
};

function CoffeeEvent () {
var events,
  __slice = Array.prototype.slice;

events = {
  events: {},
  bind: function(topic, handler, context) {
    var _base;
    if (context == null) context = this;
    return ((_base = this.events)[topic] || (_base[topic] = [])).push({
      handler: handler,
      context: context
    });
  },
  trigger: function() {
    var args, event, topic, _i, _len, _ref, _results;
    topic = arguments[0], args = 2 <= arguments.length ? __slice.call(arguments, 1) : [];
    if (this.events[topic] != null) {
      _ref = this.events[topic];
      _results = [];
      for (_i = 0, _len = _ref.length; _i < _len; _i++) {
        event = _ref[_i];
        _results.push(event.handler.apply(event.context, args));
      }
      return _results;
    }
  }
};
return events;
};
</script>
<script>
Benchmark.prototype.setup = function() {
    var e = new EventBinder();
    var c = CoffeeEvent();
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
JavaScript
e.bind('hello', function (name) {
    console.log('Hello ' + name);
});
e.trigger('Josh');
pending…
CoffeeScript
c.bind('hello', function (name) {
    console.log('Hello ' + name);
});
c.trigger('Josh');
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