with-vs-fn
JavaScript performance comparison
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.
| Test | Ops/sec | |
|---|---|---|
with |
|
pending… |
fn |
|
pending… |
You can edit these tests or add even more tests to this page by appending /edit to the URL.
1 comment
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