I’ve a simple read-only JSON object with a few propeties, I wonder if it’s worth caching its properties into the variables? Currently we use them like: jsonObject.somePropety ,I’m considering caching them into the variables like: var somePropertyValue = jsonObject.somePropety; and using that var for all future refs.
I’ve a simple read-only JSON object with a few propeties, I wonder if it’s
Share
It really depends on where jsonObject is stored, and how many scope steps (function closures) away from where it’s being used it is.
e.g. in the example below each time jsonObject.prop is accessed the code checks the scoped variables of 6 different scopes (including the global scope) before it finds jsonObject
So if the property is being accessed multiple times in the innermost function it makes sense to save a local reference to it
Now (apart from the first time the property is accessed to write it to the variable) your code has to do a lot less work to find the value.