mixin fun

JavaScript performance comparison

Revision 19 of this test case created

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;
   };
  }
 
  //6
  var asCircleCachedAndClosure = (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 = function(){
        grow.call(this, options.growBy);
    }
    this.shrink = function(){
        shrink.call(this, options.shrinkBy) ;  
    }
   }
  })();  
  //set up test constructors (one for each case to ensure fresh prototypes each time)  
  var CircularObject1 = function(radius) {
   this.radius = radius
  };
  var CircularObject2 = function(radius) {
   this.radius = radius
  };
  var CircularObject3 = function(radius) {
   this.radius = radius
  };
  var CircularObject4 = function(radius) {
   this.radius = radius
  };
  var CircularObject5 = function(radius) {
   this.radius = radius
  };
  var CircularObject6 = function(radius) {
   this.radius = radius
  };
</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
old style
for (var k in circleFns) {
 if (circleFns.hasOwnProperty(k)) {
  CircularObject1.prototype[k] = circleFns[k]
 };
}
var obj = new CircularObject1(4);
obj.shrink();
pending…
new style
asCircle.call(CircularObject2.prototype);
var obj = new CircularObject2(4);
obj.shrink();
pending…
new style w/ caching
asCircleCached.call(CircularObject3.prototype);
var obj = new CircularObject3(4);
obj.shrink();
pending…
new style w/caching and options
asCircleCachedAndCurried.call(
CircularObject4.prototype, {
 growBy: 2,
 shrinkBy: 2
});
var obj = new CircularObject4(4);
obj.shrink();
pending…
new style uncached w/options
asCircleWithOptions.call(
CircularObject5.prototype, {
 growBy: 2,
 shrinkBy: 2
});
var obj = new CircularObject5(4);
obj.shrink();
pending…
new style w/caching and closure for options
asCircleCachedAndClosure.call(
CircularObject6.prototype, {
 growBy: 2,
 shrinkBy: 2
});
var obj = new CircularObject6(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. Here’s a list of current revisions for this page:

0 comments

Add a comment