CPS vs thunks
JavaScript performance comparison
Preparation code
<script>
Benchmark.prototype.setup = function() {
var CPSFactorial = function(n, cont) {
if (n < 2) {
return cont(n);
} else {
var new_cont = function(v) {
var result = v * n;
return cont(result);
};
return CPSFactorial(n - 1, new_cont);
}
};
var thunk = function (f, lst) {
return { tag: "thunk", func: f, args: lst };
};
var thunkValue = function (x) {
return { tag: "value", val: x };
};
var thunkFactorial = function(n, cont) {
if (n < 2) {
return thunk(cont, [n]);
} else {
var new_cont = function(v) {
var result = v * n;
return thunk(cont, [result]);
};
return thunk(thunkFactorial, [n - 1, new_cont]);
}
};
var trampoline = function (thk) {
while (true) {
if (thk.tag === "value") {
return thk.val;
}
if (thk.tag === "thunk") {
thk = thk.func.apply(null, thk.args);
}
}
};
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
CPS |
|
pending… |
Thunks |
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit to the URL.
0 comments