According to Javascript’s major implementations, the lookup for variables and functions starts at the spot and traversing upwards until the global objects.
Now lets say you’re building larger OOP based frameworks and you need to call safety checks all over the place, then it doesnt seem right to call those things that way : “xapp.utils.isValidString(var). The same applies to constants, enumerations and flags which take place everywhere usally.
I am not quite sure whether this has impacts on the answer but we’re doing all in Dojo and we are pretty aware about its lang.mixin method.
However, the idea is to mixin an obvious and minimal set of functions and objects into the target objects local scope by using the constructor or prototype. Would you consider this to be a legal way ?
Then what about caching and reusing those prepared objects ? Which kind of buffer you would choose ? To me it looks rather like an ring buffer.
I am still learning JS optimization and I would be happy about your thoughts !
For your reference, here some common tips about scope managment explained by a guru : http://googlecode.blogspot.com.es/2009/06/nicholas-c-zakas-speed-up-your.html
Update : We need to focus only on modern desktop and mobile browsers, leaving IE totally out !
Also, we’re familiar with Dojo’s build chain, enabling conditional compiling per platform (If that matters).
As for identifier resolution, i.e. direct references, I recommend that you switch to the strict language (if you already haven’t), a.k.a. “strict mode”. The strict language is statically scoped, which enables JS engines to statically bind the identifiers, which results in better performance during program execution (the identifiers have already been bound during compilation).
As for property look-ups, if you have long chains, e.g.
the usual solution (not just for performance, but also to avoid code repetition) is to store the right-most object into a local variable: