Skip to content

Instantly share code, notes, and snippets.

@natefaubion
Last active January 4, 2021 18:21
Show Gist options
  • Save natefaubion/6baa45e72a0c75ee532dba584e9b73f4 to your computer and use it in GitHub Desktop.
Save natefaubion/6baa45e72a0c75ee532dba584e9b73f4 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Array slice loop</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script>
<script src="./suite.js"></script>
</head>
<body>
<h1>Open the console to view the results</h1>
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2>
</body>
</html>
"use strict";
(function (factory) {
if (typeof Benchmark !== "undefined") {
factory(Benchmark);
} else {
factory(require("benchmark"));
}
})(function (Benchmark) {
var suite = new Benchmark.Suite;
Benchmark.prototype.setup = function () {
function Slice(value0, value1, value2) {
this.value0 = value0;
this.value1 = value1;
this.value2 = value2;
}
function Cons(value0, value1) {
this.value0 = value0;
this.value1 = value1;
}
function Nil() {
}
Nil.value = new Nil();
function uncons1(slice) {
if (slice.value1 === slice.value2) {
return Nil.value
}
return new Cons(slice.value0[slice.value1], new Slice(slice.value0, slice.value1 + 1, slice.value2));
}
function Uncons(tag, value0, value1) {
this.tag = tag;
this.value0 = value0;
this.value1 = value1;
}
Uncons.Nil = new Uncons(0);
function uncons2(slice) {
if (slice.value1 === slice.value2) {
return Uncons.Nil;
}
return new Uncons(1, slice.value0[slice.value1], new Slice(slice.value0, slice.value1 + 1, slice.value2));
}
function mkUncons(tag, value0, value1) {
return { tag: tag, value0: value0, value1: value1 };
}
mkUncons.Nil = mkUncons(0);
function uncons3(slice) {
if (slice.value1 === slice.value2) {
return mkUncons.Nil;
}
return mkUncons(1, slice.value0[slice.value1], new Slice(slice.value0, slice.value1 + 1, slice.value2));
}
function mkUncons2(tag, value0, value1) {
return [tag, value0, value1];
}
mkUncons2.Nil = mkUncons2(0);
function uncons4(slice) {
if (slice.value1 === slice.value2) {
return mkUncons2.Nil;
}
return mkUncons2(1, slice.value0[slice.value1], new Slice(slice.value0, slice.value1 + 1, slice.value2));
}
var TEST_ARRAY = [];
for (var i = 0; i < 10000; i++) {
TEST_ARRAY.push(i);
}
};
suite.add("unsafeIndex", function () {
// unsafeIndex
var __done = false;
var __result;
var __sum;
var __index;
function __loop(sum, index) {
if (index === TEST_ARRAY.length) {
__done = true;
__result = sum;
} else {
__sum = sum + TEST_ARRAY[index];
__index = index + 1;
}
}
__index = 0;
__sum = 0;
while (!__done) {
__loop(__sum, __index);
}
if (__result !== 49995000) {
throw new Error("Failed!");
}
});
suite.add("instanceof", function () {
// instanceof
var __done = false;
var __result;
var __sum;
var __slice;
function __loop(sum, slice) {
var res = uncons1(slice);
if (res instanceof Nil) {
__done = true;
__result = sum;
} else if (res instanceof Cons) {
__sum = sum + res.value0;
__slice = res.value1;
} else {
throw new Error("Pattern match failure");
}
}
__slice = new Slice(TEST_ARRAY, 0, TEST_ARRAY.length);
__sum = 0;
while (!__done) {
__loop(__sum, __slice);
}
if (__result !== 49995000) {
throw new Error("Failed!");
}
});
suite.add("tag class", function () {
// tag class
var __done = false;
var __result;
var __sum;
var __slice;
function __loop(sum, slice) {
var res = uncons2(slice);
if (res.tag === 0) {
__done = true;
__result = sum;
} else if (res.tag === 1) {
__sum = sum + res.value0;
__slice = res.value1;
} else {
throw new Error("Pattern match failure");
}
}
__slice = new Slice(TEST_ARRAY, 0, TEST_ARRAY.length);
__sum = 0;
while (!__done) {
__loop(__sum, __slice);
}
if (__result !== 49995000) {
throw new Error("Failed!");
}
});
suite.add("tag record", function () {
// tag record
var __done = false;
var __result;
var __sum;
var __slice;
function __loop(sum, slice) {
var res = uncons3(slice);
if (res.tag === 0) {
__done = true;
__result = sum;
} else if (res.tag === 1) {
__sum = sum + res.value0;
__slice = res.value1;
} else {
throw new Error("Pattern match failure");
}
}
__slice = new Slice(TEST_ARRAY, 0, TEST_ARRAY.length);
__sum = 0;
while (!__done) {
__loop(__sum, __slice);
}
if (__result !== 49995000) {
throw new Error("Failed!");
}
});
suite.add("tag array", function () {
// tag array
var __done = false;
var __result;
var __sum;
var __slice;
function __loop(sum, slice) {
var res = uncons4(slice);
if (res[0] === 0) {
__done = true;
__result = sum;
} else if (res[0] === 1) {
__sum = sum + res[1];
__slice = res[2];
} else {
throw new Error("Pattern match failure");
}
}
__slice = new Slice(TEST_ARRAY, 0, TEST_ARRAY.length);
__sum = 0;
while (!__done) {
__loop(__sum, __slice);
}
if (__result !== 49995000) {
throw new Error("Failed!");
}
});
suite.on("cycle", function (evt) {
console.log(" - " + evt.target);
});
suite.on("complete", function (evt) {
console.log(new Array(30).join("-"));
var results = evt.currentTarget.sort(function (a, b) {
return b.hz - a.hz;
});
results.forEach(function (item) {
console.log((idx + 1) + ". " + item);
});
});
console.log("Array slice loop");
console.log(new Array(30).join("-"));
suite.run();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment