Skip to content

Instantly share code, notes, and snippets.

@berdario
Created May 14, 2017 23:29
Show Gist options
  • Save berdario/7ca9d015cc434428f71e77a59a521aa9 to your computer and use it in GitHub Desktop.
Save berdario/7ca9d015cc434428f71e77a59a521aa9 to your computer and use it in GitHub Desktop.
Javascript sum types performance test
function B (tag, value0, value1, value2, value3, value4) {
this.tag = tag;
this.value0 = value0;
this.value1 = value1;
this.value2 = value2;
this.value3 = value3;
this.value4 = value4;
}
function cfn (cell, a) {
if (a.tag === "B1") {
return cell.value = new B("B2", "0", a.value0 + 1);
}
if (a.tag === "B2") {
return cell.value = new B("B3", false, "0", a.value1 + 1);
}
if (a.tag === "B3") {
return cell.value = new B("B4", 0, false, "0", a.value2 + 1);
}
if (a.tag === "B4") {
return cell.value = new B("B5", "0", 0, false, "0", a.value3 + 1);
}
if (a.tag === "B5") {
return cell.value = new B("B1", a.value4 + 1);
}
}
function F (tag, values) {
this.tag = tag;
this.values = values;
}
function ffn (cell, a) {
if (a.tag === "F1") {
return cell.value = new F("F2", [0, a.values[0] + 1]);
}
if (a.tag === "F2") {
return cell.value = new F("F3", [0, 0, a.values[1] + 1]);
}
if (a.tag === "F3") {
return cell.value = new F("F4", [0, 0, 0, a.values[2] + 1]);
}
if (a.tag === "F4") {
return cell.value = new F("F5", [0, 0, 0, 0, a.values[3] + 1]);
}
if (a.tag === "F5") {
return cell.value = new F("F1", [a.values[4] + 1]);
}
}
function G (tag, values) {
this[tag] = values;
}
function gfn (cell, a) {
for (var tag in a){break}
if (tag === "G1") {
return cell.value = new G("G2", [0, a.G1[0] + 1]);
}
if (tag === "G2") {
return cell.value = new G("G3", [0, 0, a.G2[1] + 1]);
}
if (tag === "G3") {
return cell.value = new G("G4", [0, 0, 0, a.G3[2] + 1]);
}
if (tag === "G4") {
return cell.value = new G("G5", [0, 0, 0, 0, a.G4[3] + 1]);
}
if (tag === "G5") {
return cell.value = new G("G1", [a.G5[4] + 1]);
}
}
var cell = { value: new B("B1", 0) };
for (var i = 0; i < 100000; i++) {
cfn(cell, cell.value);
}
var cell = { value: new F("F1", [0]) };
for (var i = 0; i < 100000; i++) {
ffn(cell, cell.value);
}
var cell = { value: new G("G1", [0]) };
for (var i = 0; i < 100000; i++) {
hfn(cell, cell.value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment