3

To clarify my question,

I have a large chunk of code which accesses properties of JS objects which at some times is nested inside multiple other objects i.e,

object.array1[0].anotherObject.finalObject.someProperty

This property is referenced multiple times during execution. Assuming there are no concurrency problems, is it more efficient to assign this property to a variable to test values against it this way,

var prop = object.array1[0].anotherObject.finalObject.someProperty

The question boils down to me being unfamiliar with how referencing works in JS. Is there a point where the overhead for creating and assigning a variable is more efficient than accessing an object's property multiple times. If so, is there a good rule of thumb to follow when deciding which to use?

SHolmes
  • 178
  • 13
  • 3
    Why should performance even matter? Are you at a point where you accessing so many times it’s noticeably affecting performance? If not, does it matter? IMO it boils down to readability. – Andrew Li Oct 08 '17 at 20:31
  • 1
    [Yes](https://stackoverflow.com/q/8288664/1048572). Also, regardless of "performance": write DRY and readable code. – Bergi Oct 08 '17 at 20:33
  • 1
    if you do not need to assign a value to the property, you could use another variable. – Nina Scholz Oct 08 '17 at 20:34
  • If your script has huge size then you should think startup performance browser try to render your script hence you can use variable it is represent as like pointer it just take memory for your variable also you can read this article https://addyosmani.com/blog/javascript-startup-performance/ – Ferhat BAŞ Oct 08 '17 at 20:42
  • @AndrewLi I'm asking more out of curiosity than anything else. – SHolmes Oct 08 '17 at 20:44
  • @Bergi thanks for the link, it was very informative – SHolmes Oct 08 '17 at 20:44
  • @FerhatBAŞ thanks for the article it's an interesting read! – SHolmes Oct 08 '17 at 20:48

0 Answers0