When considering the "worth" of this issue, it all depends on your perspective.
Caching properties may provide some potential performance improvements if the same property is used repeatedly, but unless you are looping through thousands of times, the difference may not be noticeable to the user.
However, caching properties can enhance the aesthetics of your code. For example, if you find yourself referencing the same property multiple times within a specific block of code, caching it at the start could streamline the process - especially in cases involving nested objects like this:
jsonObject.nestedObject.doSomething();
jsonObject.nestedObject.doAnotherThing();
alert(jsonObject.nestedObject.someProperty);
//compared with:
var nObj = jsonObject.nestedObject;
nObj.doSomething();
nObj.doAnotherThing();
alert(nObj);
I personally believe that the latter method is more efficient and easier to read (assuming the variable nObj
is given a more descriptive name, although this is just a general example).
On the other hand, I do not recommend caching properties in global variables for widespread use throughout your codebase. It's best practice to limit global variables for various reasons, such as avoiding conflicts with external libraries and preventing obscure bugs caused by multiple functions modifying the same variable. Instead, opt for caching within individual functions that heavily utilize a specific property - keep in mind that JavaScript has function scope, not block scope. (If a particular function frequently references a certain property, consider making it an argument for cleaner code.)
P.S. In JavaScript, there are no JSON objects, only objects.