Consider an object structured as follows:
var foo = { a: { b: { c: { d: { e: { f: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ] } } } } } };
We have functions that use different parts of this object like so:
function doStuff() {
if ( foo.a.b.c.d.e.f[ 5 ] >= some_important_filter_value ) {
alert( "Some important filter value is " + foo.a.b.c.d.e.f[ 5 ] +
"!!\nRun for your lives!!" );
}
}
The values in foo
are not expected to change frequently during function execution.
Is it more efficient to read the values from the object and store them as variables/constants, or is it acceptable to use the full path through the object foo
each time?
For example,
function doStuff() {
var mem = foo.a.b.c.d.e.f[ 5 ];
if ( mem >= some_important_filter_value ) {
alert( "Some important filter value is " + mem +
"!!\nRun for your lives!!" );
}
}
Which approach is considered better and why?
My testing in Google Chrome's JS console reveals that running loops with deep object queries compared to using temporary variable references showed similar performance. Does creating temporary variables bring any other advantages besides potential code/file size reduction and improved readability?
Could referencing the value directly from the object be more efficient than storing it in a temporary variable?
Why would var temp = foo.a.b.c...
be preferred over using the reference we already have?
Ultimately, does the number of dots in the reference impact the efficiency, or is it negligible in the long run?