views:

145

answers:

3
+4  A: 

By looking at your comment seems that your major concern is having to reference several times a deeply nested object, avoiding eval and with I would simply recommend you to use an alias identifier, for example:

 // some local scope...
 var foo = namespace.constructors.blah.dramatic.variables;
 // replace foo with something meaningful :)
 foo.method1();
 foo.method2();
 foo.property1;
 // etc...

In that way the deeply nested object will already be resolved and referencing your alias will be faster, eval and with IMO would only cause you more problems than benefits in this case.

CMS
Thanks. I considered that, of course. But, given that the variables names are known; instead of using an alias I would just repeat what eval does. Like, var a = variables.a; var b = variables.b; and so on... Which I believe is the most efficient method, but not as elegant as I would like. eval saves some bytes but seems like a hack. Setting a dozen variables with eval shouldn't be noticeable performance wise.
Carlos Gil
@Carlos Gil Saving "bytes" is a worthless endeavor. If you want to optimize your code to by decreasing scope chain lookup, what @CMS has is the way to go. Also, `eval` (and sometimes `with`) is slow, and the side effects of an improperly used `with` make it not worth using.
Justin Johnson
@Justin Johnson I am quite sure that after initially setting the local variables they are accessed faster than the properties of one of them. Meaning; foo* are accessed faster than foo.property*. If I am terrible wrong, please explain.
Carlos Gil
Premature optimization is the .. something. Using "sav[ing] some bytes" as a reason is thinking in this mindset.
strager
@strager "saves some bytes" was not a reason, but a statement.
Carlos Gil
@Carlos, I would go with your options 1 or 3, check this simple [performance test](http://jsbin.com/uyani3/13/).
CMS
@CMS Nice. In IE8, option 2 is the fastest, seems like the with statement is much more optimized in IE than in Firefox. My script is meant to be run on IE only and in IE7 compatibility mode, but the benchmarking script does not work on IE7. Also, the test should be divided into 2 parts, (a) Setting variables; (b) Accessing variables. (b) is more important because it's much more recurrent, in my script at least. I'll make a test for this. Thanks.
Carlos Gil
@Carlos Gil You misunderstand me. I am saying that you should decrease scope chain lookups by creating a local variable.
Justin Johnson
@Justin Johnson well, I understood that you endorsed CMS's answer (option 1) which does decrease scope chain lookups, but not as much as option 3 of the original post.
Carlos Gil
@Carlos Gil This is a micro optimization, so it really doesn't matter outside of readability, but count along with me: Example #3 has 9 lookups and 3 assignments whereas Example #1 has 6 lookups and 1 assignment.
Justin Johnson
@Justin Johnson, I guess it's my fault for not expanding the examples. After the initial assignments, the variables are meant to be looked up thousands of times, as I thought I clarified in the comment with "...and the content of variables is referenced a lot".
Carlos Gil
@Justin Johnson, by the way example #1 has 7 lookups...
Carlos Gil
A: 

One alternative is to make the object the current scope. It won't make the properties local variables, but you can access them using the this keyword:

var variables = { a:42, b:1337 };

(function(){
  alert(this.a);
  alert(this.b);
}).apply(variables);

This has the advantage that you are not copying anything anywhere, you are accessing the properties directly.

Guffa
How could this possibly be better than just assigning `variables` a shorter name. You'd still be accessing the properties directly.
MooGoo
@MooGoo: No, if you copy the properties to local variables you will not access the properties directly. If you change the local variable, the property won't change.
Guffa
I said "assigning `variables` a shorter name", i.e. `var v = variables`. `v` holds a reference to the same object that `variables` references which is no different than `this` holding that reference, except it is more concise and does not require an unnecessary function call (which is probably as slow if not slower than using `with`).
MooGoo
@MooGoo: Ok, I see what you mean. No, it's not better, which I never said that it was, it's just another way of doing it.
Guffa
A: 

Well, I'll post the answer myself.

No.

There is no way to set local variables without knowing the name of the variable without using eval()...

...But using local variables (option 3) is the best way.

Carlos Gil