Monomorphic vs Polymorphic Records

Benchmark created by Victor Savkin on


Setup

function Record1(n) {
      this.record1Field = 1;
      this.next = n;
      this.value = 1;
    }
    
    function Record2(n) {
      this.record2Field = 1;
      this.next = n;
      this.value = 2;
    }
    
    function Record3(n) {
      this.record3Field = 1;
      this.next = n;
      this.value = 3;
    }
    
    function Record4(n) {
      this.record4Field = 1;
      this.next = n;
      this.value = 4;
    }
    
    function Record5(n) {
      this.record5Field = 1;
      this.next = n;
      this.value = 5;
    }
    
    function createRecord(i, m) {
      var r = i % m;
      if (r === 0) return new Record1();
      if (r === 1) return new Record2();
      if (r === 2) return new Record3();
      if (r === 3) return new Record4();
      if (r === 4) return new Record5();
      throw "BOOM";
    }
    
    var record1 = [new Record1()];
    var record2 = [new Record1()];
    var record3 = [new Record1()];
    var record4 = [new Record1()];
    var record5 = [new Record1()];
    
    for(var i = 0; i < 300; ++i) {
      var n = createRecord(i, 1);
      record1[record1.length - 1].next = n;
      record1.push(n);
    
      n = createRecord(i, 2);
      record2[record2.length - 1].next = n;
      record2.push(n);
    
      n = createRecord(i, 3);
      record3[record3.length - 1].next = n;
      record3.push(n);
    
      n = createRecord(i, 4);
      record4[record4.length - 1].next = n;
      record4.push(n);
    
      n = createRecord(i, 5);
      record5[record5.length - 1].next = n;
      record5.push(n);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
1 Record Type
var r = record1;
var sum = 0;
while (r != null) {
  sum += r.value;
  r = r.next;
}
ready
2 Record Types
var r = record2;
var sum = 0;
while (r != null) {
  sum += r.value;
  r = r.next;
}
ready
3 Record Types
var r = record3;
var sum = 0;
while (r != null) {
  sum += r.value;
  r = r.next;
}
ready
4 Record Types
var r = record4;
var sum = 0;
while (r != null) {
  sum += r.value;
  r = r.next;
}
ready
5 Record Types
var r = record5;
var sum = 0;
while (r != null) {
  sum += r.value;
  r = r.next;
}
ready

Revisions

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

  • Revision 1: published by Victor Savkin on