Function Inception

JavaScript performance comparison

Test case created by kpozin

Info

Related StackOverflow question

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var literal = function literal(a) { var sum = 0; for (var i = 0; i < 500; ++i) { sum += Math.random() * a; } return sum; }
   
    var sBody = "var sum = 0; for (var i = 0; i < 500; ++i) { sum += Math.random() * a; } return sum;";
   
    var meta = new Function("a", sBody);
   
    var metaMetaMaker = function(sName, sArgs, sBody) {
        return new Function("return function " + sName + "(" + sArgs + ") {" + sBody + "}")();
    }
   
    var metaMeta = metaMetaMaker("literal", "a", sBody);
   
    var evaled;
    eval("evaled = " + literal.toString());
   
    var x;
};
</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
literal
x = literal(5);
pending…
meta
x = meta(5);
pending…
meta-meta
x = metaMeta(5);
pending…
eval'ed
x = evaled(5);
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