Defining methods and properties directly or use getters in js

Viewed 51

I am developing some JS code and have a performance question. Consider that I have a big object which has many methods and properties:

const obj = {
  prop1: 'some value',
  ...
  prop1000: 'some value',
  method1: someMethod,
  ...
  method1000: someMethod,
}

first solution
I guess that if I use getters to fetch each field (prop or method) it will help the runtime engine to load fewer fields in memory when not necessary. For example:

const obj1 = {
  get prop1() { return 'some value'; },
  ...
  get prop1000() { return 'some value'; },
  get method1() { return someMethod; },
  ...
  get method1000() { return someMethod; },
}

second solution
and note that in this scenario I don't need any setter.
So when I use the second solution when I fetch the obj, no fields are fetched automatically until I directly call a getter.
And now the question is, Does the second solution really improve the memory usage and runtime performance, or is it at least a good practice?

0 Answers
Related