inheritance-vs-aggregation (v3)

Revision 3 of this benchmark created on


Preparation HTML

<script>
  function inherit(ctor, super_) {
   ctor.super_ = super_;
   ctor.prototype = Object.create(super_.prototype);
  }
  
  function mkChild(super_) {
   function child() {
    super_.call(this);
   }
   inherit(child, super_);
   return child;
  }
  
  function Parent() {
   this.prop = 0;
  }
  Parent.prototype.incProp = function() {
   this.prop++;
  };
  
  var children = [];
  
  for (var i = 0; i < 10; i++) {
   var ctor = mkChild(Parent);
   children.push(new ctor());
  }
  
  function Part() {
   this.prop = 0;
  }
  Part.prototype.incProp = function() {
   this.prop++;
  };
  
  function mkWhole() {
   function Whole() {
    this.part = new Part();
   }
  
   Whole.prototype.incProp = function() {
    this.part.incProp();
   };
  
   return Whole;
  }
  
  var wholes = [];
  
  for (var i = 0; i < 10; i++) {
   var ctor = mkWhole();
   wholes.push(new ctor());
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
inheritance
children[0].incProp();
children[1].incProp();
children[2].incProp();
children[3].incProp();
children[4].incProp();
children[5].incProp();
children[6].incProp();
children[7].incProp();
children[8].incProp();
children[9].incProp();
ready
aggregation
wholes[0].incProp();
wholes[1].incProp();
wholes[2].incProp();
wholes[3].incProp();
wholes[4].incProp();
wholes[5].incProp();
wholes[6].incProp();
wholes[7].incProp();
wholes[8].incProp();
wholes[9].incProp();
ready

Revisions

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