monomorphism vs polymorphism (v2)

Revision 2 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++) result += f(mono[i], i);
ready
calling polymorphic
// polymorphic but with matching property offsets 
for (var i = 0; i < N; i++) result += f(poly[i], i);
ready
calling megamorphic
for (var i = 0; i < N; i++) result += f(mega[i], i);
ready
poly (different property offsets)
// polymorphic but with different property offsets
for (var i = 0; i < N; i++) result += f(poly2[i], i);
ready

Revisions

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