Function constructor context switching

Benchmark created by Sandro Pasquali on


Description

Whether it is faster to call() a Function (Function.call), or pass context as argument (Function(arg))

Setup

var fA = function(body) {
       return Function("with(this) { return (function(){" + body + "})(); };")
    }
    
    var fB = function(body) {
       return new Function("a", "with(a) { return (function(){" + body + "})(); };")
    }
    
    var targ = "var a = 1;"

Test runner

Ready to run.

Testing in
TestOps/sec
With call(this)
for(var i=0; i < 10000; i++) {
  var f = fA(targ);
  f.call({ a: 0 })
}
ready
With arguments
for(var i=0; i < 10000; i++) {
  var f = fB(targ);
  f({ a: 0 })
}
ready
With apply(this)
for(var i=0; i < 10000; i++) {
  var f = fA(targ);
  f.apply({ a: 0 })
}
ready

Revisions

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

  • Revision 1: published by Sandro Pasquali on