How expensive are local variables (var v), global variables (window.v) and cross-global variables (parent.v) in JavaScript, in the major browsers? Has anyone performed any good tests on this one?
How expensive are local variables ( var v ), global variables ( window.v )
Share
Ignoring interpreter/parser pros and cons, all that matters is how much the runtime has to look at parts of the scope chain.
Consider this simple example:
Remember that functions share the same namespace as variables (I think) and act like variables with regards to resolution.
There is no additional cost to using
foo['bar']instead offoo.bar, except perhaps optimizations by the Javscript engine (though if you’re optimizing the latter it should be easy to optimize the former if the contents are literal).