with-vs-fn

JavaScript performance comparison

Test case created

Preparation code

<script>
  locals = {
   name: "Matt",
   email: "wahoo@wahoo.com",
   phone: "(555) 555-5555"
  };
 
 
  keys = [];
  values = [];
 
  for (key in locals) {
   local = locals[key];
 
   keys.push(key);
   values.push(local);
 
  }
 
  keys = keys.join(',');
 
  function withwith() {
 
   with(locals) {
 
    var t = name + email + phone,
        y = phone + email + name,
        h = phone + name;
 
   }
 
  }
 
  fn = new Function(keys, "\
   var t = name + email + phone,\
       y = phone + email + name,\
       h = phone + name;\
  "
);
</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
with
withwith();
pending…
fn
fn.apply(this, values);
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

1 comment

Mark Murphy commented :

I generated a different test which dynamically generates the function params to more closely simulate the flexibility of the "with" statement.

http://jsperf.com/dynamic-fn-with-vs-func

Add a comment