gfsdgfsdgfdsgfdsgfdsgfdsgfd

JavaScript performance comparison

Test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function EventDispatcher(events) {
      this.__callbacks__ = events || {};
    }
   
    EventDispatcher.prototype.subscribe = function(name, callback, self) {
      var data = {
        callback: callback,
        self: self || this
      };
   
      if (!this.__callbacks__[name]) {
        this.__callbacks__[name] = [data];
      } else {
        this.__callbacks__[name].push(data);
      }
    };
   
    EventDispatcher.prototype.unsubscribe = function(name, callback) {
      var callbacks = this.__callbacks__[name];
   
      if (callbacks) {
        for (var i = 0, length = callbacks.length; i < length; ++i) {
          if (callbacks[i].callback === callback) {
            //Remove the callback from the array by replacing it
            //with the last element and decrementing the length.
            callbacks[i] = callbacks[length - 1];
            --callbacks.length;
            break;
          }
        }
      }
    };
   
    EventDispatcher.prototype.dispatch = function(name, data) {
      var callbacks = this.__callbacks__[name];
   
      if (callbacks) {
        for (var i = 0, length = callbacks.length; i < length; ++i) {
          callbacks[i].callback.call(callbacks[i].self, data);
        }
      }
    };
   
    function EventDispatcher2(events) {
      this.__callbacks__ = events || {};
    }
   
    EventDispatcher2.prototype.subscribe = function(name, callback) {
   
      if (!this.__callbacks__[name]) {
        this.__callbacks__[name] = [callback];
      } else {
        this.__callbacks__[name].push(callback);
      }
    };
   
    EventDispatcher2.prototype.unsubscribe = function(name, callback) {
      var callbacks = this.__callbacks__[name];
   
      if (callbacks) {
        for (var i = 0, length = callbacks.length; i < length; ++i) {
          if (callbacks[i] === callback) {
            //Remove the callback from the array by replacing it
            //with the last element and decrementing the length.
            callbacks[i] = callbacks[length - 1];
            --callbacks.length;
            break;
          }
        }
      }
    };
   
    EventDispatcher2.prototype.dispatch = function(name, data) {
      var callbacks = this.__callbacks__[name];
   
      if (callbacks) {
        for (var i = 0, length = callbacks.length; i < length; ++i) {
          callbacks[i].call(this, data);
        }
      }
    };
};
</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
ed
var ed = new EventDispatcher({
  evt: null
});
ed.subscribe('evt', function() {});
ed.subscribe('evt', function() {});
ed.subscribe('evt', function() {});
ed.dispatch('evt');
pending…
ed2
var ed2 = new EventDispatcher2({
  evt: null
});
ed2.subscribe('evt', function() {});
ed2.subscribe('evt', function() {});
ed2.subscribe('evt', function() {});
ed2.dispatch('evt');
pending…

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

Compare results of other browsers

0 comments

Add a comment