function(){} vs new Function() vs eval(function(){}) (v18)

Revision 18 of this benchmark created on


Description

https://developer.mozilla.org/en/JavaScript/Reference/Functions_and_function_scope says

Functions defined by function expressions and function declarations are parsed only once, while those defined by the Function constructor are not. That is, the function body string passed to the Function constructor must be parsed every time it is evaluated. Although a function expression creates a closure every time, the function body is not reparsed, so function expressions are still faster than "new Function(...)". Therefore the Function constructor should be avoided whenever possible.

Which sounds bad.

Preparation HTML

<script>
</script>

Setup

var pGet = function(){return this.scope[arguments.callee.prop];};

Test runner

Ready to run.

Testing in
TestOps/sec
Function
var id = "test",
t = {
scope:{test:':)'},
                    pGet : new Function("return this.scope."+id+";"),
};
for (var i=0;i<1000;i++)
t.pGet();
ready
aFunction
var id = "test",
t = {
scope:{test:':)'},
                    pGet : pGet,
};
t.pGet.prop = id;
for (var i=0;i<1000;i++)
t.pGet();
ready
afunc+scope
var id = "test",
t = {
scope:{test:':)'},
                    pGet : function(){return this.scope[id];},
};
for (var i=0;i<1000;i++)
t.pGet();
ready

Revisions

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