Skip to content

Instantly share code, notes, and snippets.

@dmtrKovalenko
Created April 18, 2020 19:42
Show Gist options
  • Save dmtrKovalenko/b48f8cfc9f3af51c0b1423b13c7ed3b6 to your computer and use it in GitHub Desktop.
Save dmtrKovalenko/b48f8cfc9f3af51c0b1423b13c7ed3b6 to your computer and use it in GitHub Desktop.
Benchmark displaying slowness of `caml_compare`
const Benchmark = require("benchmark");
const Caml_obj = require("bs-platform/lib/js/caml_obj.js");
const Belt_SortArray = require("bs-platform/lib/js/belt_SortArray.js");
const Js_math = require("bs-platform/lib/js/js_math.js");
const suite = new Benchmark.Suite("arrays", {
minSamples: 1000,
});
function sortNumbers(a, b) {
if (Caml_obj.caml_greaterthan(a, b)) {
return 1;
} else if (Caml_obj.caml_lessthan(a, b)) {
return -1;
} else {
return 0;
}
}
var arr = Array(1000)
.fill(0)
.map((_) => Js_math.random_int(1, 1000));
suite
.add("Js.Array camp_compare", () => {
arr.sort(sortNumbers);
})
.add("Js.Array no caml_compare", () => {
arr.sort((a, b) => a - b);
})
.add("Belt.Array camp_compare", () => {
Belt_SortArray.stableSortBy(arr, sortNumbers);
})
.add("Belt.Array no camp_compare", () => {
Belt_SortArray.stableSortBy(arr, (a, b) => a - b);
})
.on("cycle", (event) => {
console.log(String(event.target));
})
.on("complete", function () {
console.log("Fastest is " + this.filter("fastest").map("name"));
})
.run({ async: true });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment