monomorphism vs polymorphism

JavaScript performance comparison

Revision 3 of this test case created by elkorn

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  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;

};

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