mixin fun

JavaScript performance comparison

Revision 52 of this test case created by

Preparation code

<script>
  //1
  var circleFns = {
   area: function() {
    return Math.PI * this.radius * this.radius;
   },
   grow: function() {
    this.radius++;
   },
   shrink: function() {
    this.radius--;
   }
  }
  
  //2
  var asCircle = function() {
   this.area = function() {
    return Math.PI * this.radius * this.radius;
   };
   this.grow = function() {
    this.radius++;
   };
   this.shrink = function() {
    this.radius--;
   };
  }
  
  //3
  var asCircleCached = (function() {
   var area = function() {
    return Math.PI * this.radius * this.radius;
   };
   var grow = function() {
    this.radius++;
   };
   var shrink = function() {
    this.radius--;
   };
   return function() {
    this.area = area, this.grow = grow, this.shrink = shrink;
   }
  })();
  
  //4
  Function.prototype.curry = function() {
   var fn = this;
   var args = [].slice.call(arguments, 0);
   return function() {
    return fn.apply(this, args.concat([].slice.call(arguments, 0)));
   }
  }
  
  //4
  var asCircleCachedAndCurried = (function() {
   var area = function() {
    return Math.PI * this.radius * this.radius;
   };
   var grow = function(growBy) {
    this.radius += growBy;
   };
   var shrink = function(shrinkBy) {
    this.radius -= shrinkBy;
   };
   return function(options) {
    this.area = area, this.grow = grow.curry(options['growBy']), this.shrink = shrink.curry(options['shrinkBy'])
   }
  })();
  
  //5
  var asCircleWithOptions = function(options) {
   this.area = function() {
    return Math.PI * this.radius * this.radius;
   };
   this.grow = function() {
    this.radius += options.growBy;
   };
   this.shrink = function() {
    this.radius -= options.shrinkBy;
   };
  }
  
  //set up test constructor
  var CircularObject = function(radius) {
   this.radius = radius
  };

var strf = "", strf2 = "", strf3="";
var fnsA = [], fnsP = [], fnsN = [];
var fns = circleFns;
var K = 0;
for (var l in fns)
{
        var f = fns[l];
        fnsA.push(f);
        fnsP.push(l);
        fnsN.push('fnsA['+ K +']');
        strf += "this." + l + " = fnsA[" + K + "]; ";
        strf2 += "o." + l + " = " + l+ "; ";
        strf3 += "this." + l + " = " + l + "; ";

        K++;
}


var Maker = new Function(strf);

var meta = "var Maker2 = new Function('" + fnsP.join("', '") + "', 'o', strf2); ";
meta += "var Maker3 = Maker2.bind(null, " + fnsN.join(", ") + ");";
eval(meta);

var meta2 = [
	'var Maker4 = (function(',
		fnsP.join(", "),
	') { ',
		'return function () { ',
			strf3,
		'};',
	'})(',
		fnsN.join(", "),
	');'
].join(' ');
eval(meta2);

var meta3 = [
	'var Maker5 = (function(',
		fnsP.join(", "),
	') { ',
		'return function (o) { ',
			strf2,
		'};',
	'})(',
		fnsN.join(", "),
	');'
].join(' ');
eval(meta3);


var meta4 = [
	'var Maker6 = (function(',
		fnsP.join(", "),
	') { ',
		'return function (options) { ',
			"this.area = area, this.grow = grow.bind(this, options['growBy']), this.shrink = shrink.bind(this, options['shrinkBy'])",
		'};',
	'})(',
		fnsN.join(", "),
	');'
].join(' ');
eval(meta4);

</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
old style
var obj = new CircularObject(4);
for (var k in circleFns) {
 if (circleFns.hasOwnProperty(k)) {
  var pd = Object.getOwnPropertyDescriptor(circleFns, k);
      if (pd) {
        Object.defineProperty(obj, k, pd);
      }
   };
}
obj.shrink();
pending…
new style
asCircle.call(CircularObject.prototype);
var obj = new CircularObject(4);
obj.shrink();
pending…
new style w/ caching
asCircleCached.call(CircularObject.prototype);
var obj = new CircularObject(4);
obj.shrink();
pending…
new style w/caching and options
asCircleCachedAndCurried.call(
CircularObject.prototype, {
 growBy: 2,
 shrinkBy: 2
});
var obj = new CircularObject(4);
obj.shrink();
pending…
new style uncached w/options
asCircleWithOptions.call(
CircularObject.prototype, {
 growBy: 2,
 shrinkBy: 2
});
var obj = new CircularObject(4);
obj.shrink();
pending…
with Function()
Maker.call(CircularObject.prototype);
var obj = new CircularObject(4);
obj.shrink();
pending…
meta
Maker3(CircularObject.prototype);
var obj = new CircularObject(4);
obj.shrink();
pending…
meta2
Maker4.call(CircularObject.prototype);
var obj = new CircularObject(4);
obj.shrink();
pending…
meta3
Maker5(CircularObject.prototype);
var obj = new CircularObject(4);
obj.shrink();
pending…
metacurry
Maker6.call(CircularObject.prototype, {
 growBy: 2,
 shrinkBy: 2
});
var obj = new CircularObject(4);
obj.shrink();
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