monomorphism vs polymorphism (v12)

Revision 12 of this benchmark created by John Smith on


Preparation HTML

<script>
var UID = 0;
</script>

Setup

// Need to generate core of the benchmark dynamically to ensure
    // that type feedback does not leak between individual sample runs.
    var code = " /* " + (UID++)  + " */ " +  " var dummy=new Map(); for (var i = 0; i < N; i++) {" +
    "  var obj = arr[i];" + 
    "  dummy.set(obj, 'value');" + 
    "}" + 
    "return result";
    
    var f = new Function("N, arr, result", code);
    
    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
result = f(N, mono, result);
ready
calling polymorphic
// polymorphic but with matching property offsets 
result = f(N, poly, result);
ready
calling megamorphic
result = f(N, mega, result);
ready
poly (different property offsets)
// polymorphic but with different property offsets
result = f(N, poly2, result);
ready

Revisions

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