mixin fun (v43)

Revision 43 of this benchmark created on


Preparation HTML

<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
  var asCircleCached2 = (function() {
   return function() {
    this.area = function() {
        return Math.PI * this.radius * this.radius;
    };
    this.grow = function() {
        this.radius++;
     };
    this.shrink = function() {
        this.radius--;
    };
   }
  })();

  //5
  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)));
   }
  }
  
  //5
  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'])
   }
  })();
  
  //6
  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
  };
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
old style
for (var k in circleFns) {
 if (circleFns.hasOwnProperty(k)) {
  CircularObject.prototype[k] = circleFns[k]
 };
}
var obj = new CircularObject(4);
obj.shrink();
ready
new style
asCircle.call(CircularObject.prototype);
var obj = new CircularObject(4);
obj.shrink();
ready
new style w/ caching
asCircleCached.call(CircularObject.prototype);
var obj = new CircularObject(4);
obj.shrink();
ready
new style w/ caching2
asCircleCached2.call(CircularObject.prototype);
var obj = new CircularObject(4);
obj.shrink();
ready
new style w/caching and options
asCircleCachedAndCurried.call(
CircularObject.prototype, {
 growBy: 2,
 shrinkBy: 2
});
var obj = new CircularObject(4);
obj.shrink();
ready
new style uncached w/options
asCircleWithOptions.call(
CircularObject.prototype, {
 growBy: 2,
 shrinkBy: 2
});
var obj = new CircularObject(4);
obj.shrink();
ready

Revisions

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