mixin fun

JavaScript performance comparison

Revision 58 of this test case created by Alexey

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;
   };
  }
  
function AsCircleClass() {}

AsCircleClass.prototype.area = function() {
  return Math.PI * this.radius * this.radius;
};

AsCircleClass.prototype.grow = function() {
  this.radius++;
};

AsCircleClass.prototype.shrink = function() {
  this.radius--;
};


  //set up test constructor
  var CircularObject = function(radius) {
   this.radius = radius
  };

  //set up test constructor for class inheritance
  var CircularObjectClass = function(radius) {
   AsCircleClass.call(this);
   this.radius = radius
  };

CircularObjectClass .prototype = Object.create(AsCircleClass.prototype);
CircularObjectClass .prototype.constructor = CircularObjectClass ;

asCircleCached.call(CircularObject.prototype);
</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
new style w/ caching
var obj = new CircularObject(4);
obj.shrink();
pending…
classical inheritance
var obj = new CircularObjectClass(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