JSOverloading

JavaScript performance comparison

Test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function functions(fs) {
  	var self = {};
  	fs.forEach(function(f) {
  		self[f.length] = f
  	});
  	return function() {
  		(self[arguments.length] || function(){}).apply(this, arguments);
  	};
  }
  
  function functions2(fs) {
  	var self = [];
  	fs.forEach(function(f) {
  		self[f.length] = f
  	});
  	return function() {
  		(self[arguments.length] || function(){}).apply(this, arguments);
  	};
  }
  
  function addMethod(object, name, fn) {
    var old = object[name];
    object[name] = function() {
      if (fn.length == arguments.length) return fn.apply(this, arguments);
      else if (typeof old == 'function') return old.apply(this, arguments);
    };
  }
  
  function addMethod2(object, name, fn) {
    object._store = object._store || {};
    object._store[name] = object._store[name] || {};
    object._store[name][fn.length] = fn;
    object[name] = function() {
      if (this._store[name][arguments.length]) return this._store[name][arguments.length].apply(this, arguments);
    };
  }
  

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
caelum - every arity is required
function Users4() {
  this.add = functions2([

  function() {}, function(name) {}, function(name, age) {}]);
}
new Users4().add();
new Users4().add("1");
new Users4().add("1", "2");
pending…
resig
function Users3() {
  addMethod(this, "add", function() {});
  addMethod(this, "add", function(name) {});
  addMethod(this, "add", function(first, last) {});
}

new Users3().add();
new Users3().add("1");
new Users3().add("1", "2");
pending…
another guy
function Users2() {
  addMethod2(this, "add", function() {});
  addMethod2(this, "add", function(name) {});
  addMethod2(this, "add", function(first, last) {});
}

new Users2().add();
new Users2().add("1");
new Users2().add("1", "2");
pending…
caelum
function Users() {
  this.add = functions([

  function() {}, function(name) {}, function(name, age) {}]);
}
new Users().add();
new Users().add("1");
new Users().add("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.

0 Comments