monomorphism vs polymorphism

JavaScript performance comparison

Revision 11 of this test case created by

Preparation code

<script>
var UID = 0;
</script>
      
<script>
Benchmark.prototype.setup = function() {
  // Need to generate core of the benchmark dynamically to ensure
  // that type feedback does not leak between individual sample runs.
  var code = " /* " + (UID++)  + " */ " +  " for (var i = 0; i < N; i++) {" +
  "  var obj = arr[i];" + 
  "  result += obj.x + obj.y + i;" + 
  "}" + 
  "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;

};

Benchmark.prototype.teardown = function() {
  if (result < 0) throw "";

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
calling monomorphic function
result = f(N, mono, result);
pending…
calling polymorphic
// polymorphic but with matching property offsets 
result = f(N, poly, result);
pending…
calling megamorphic
result = f(N, mega, result);
pending…
poly (different property offsets)
// polymorphic but with different property offsets
result = f(N, poly2, result);
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.

0 Comments