Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have a use case where I need to create say two javascript objects & use their

ID: 647366 • Letter: I

Question

I have a use case where I need to create say two javascript objects & use their properties in one another. eg -

var Object1 = {
settings: {
   property1: 'someValue',
   property2: 'someValue'
}
}

var Object2 = {
foreignProperty: Object1.settings.property1;
}
I wanted to know if its alright to use a reference object for settings if I know that I will be using the settings property a lot. eg-

var Object1Settings,
    Object1 = {
      settings: {
      property1: 'someValue',
      property2: 'someValue'
    }
}
var Object1Settings = Object1.settings;

var Object2 = {
foreignProperty: Object1Settings.property1;
}
Is this approach acceptable in terms of right ways of coding & performance?

Thanks

Explanation / Answer

Not only it increases readability, but such code also runs way faster.

Generally when programming I believe this is the order of priorities:

Readibility
CPU performance
Saving memory
Based on this you can see, that caching any value is probably a good idea. It costs memory and saves you CPU cycles.

But I hope you do know that changing Object2.foreignProperty will render original Object1.settings.property1 unchanged.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote