CPS vs thunks

JavaScript performance comparison

Test case created

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.

Testing in unknown unknown
Test Ops/sec
CPS
CPSFactorial(5, function(v) {
    return v;
});
pending…
Thunks
trampoline(thunkFactorial(5, thunkValue));
pending…

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

Compare results of other browsers

0 comments

Add a comment