monomorphism vs polymorphism (v10)

Revision 10 of this benchmark created on


Setup

var f = function(obj, step){
      return obj.x + obj.y + step;
  }
  
  var K = 10;
  var objs = [];
  
  for (var i = 0; i < (K / 2); i++) {
    objs.push(new function () {
      this.x = 0;
      this.y = 0;
    } ());
  }
  
  
  for (var i = 0; i < (K / 2); i++) {
    objs.push(new function () {
      this.y = 0;    
      this.x = 0;
    } ());
  }
  
  var mono = [],
      poly = [],
      poly2 = [],
      mega = [];
  
  var N = 20;
  for (var i = 0; i < N; i++) {
    mono.push(objs[0])
    poly.push(objs[i % 3])
    poly2.push(objs[(i % 3) || (K / 2)])
    mega.push(objs[i % 6])
  }
  
  var result = 0;

Teardown



            if (result < 0) throw "";
        
  

Test runner

Ready to run.

Testing in
TestOps/sec
calling monomorphic function
for (var i = 0; i < N; i++) {
  var obj = mono[i];
  result += obj.x + obj.y + i;
}
ready
calling polymorphic
// polymorphic but with matching property offsets 
for (var i = 0; i < N; i++) {
  var obj = poly[i];
  result += obj.x + obj.y + i;
}
ready
calling megamorphic
for (var i = 0; i < N; i++) {
  var obj = mega[i];
  result += obj.x + obj.y + i;
}
ready
poly (different property offsets)
// polymorphic but with different property offsets
for (var i = 0; i < N; i++) {
  var obj = poly2[i];
  result += obj.x + obj.y + i;
};
ready

Revisions

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