I'm my scenario I have to create a lot of objects (100k+). Those objects can 'evolve' in the course if their life. Some will stay practically unchanged, others with will mutate and have a bunch of props assigned.
The most evolved objects might have 30+ properties.
All object are just data, no function, no logic.
From performance point of view, should I use:
const obj = Object.create(null)
obj.prop1 = ...;
obj.prop2 = ...;
//later
obj.prop3 = ...;
//later still
obj.prop4 = ...;
or
cost obj = {
prop1 = ...;
prop2 = ...;
prop3 = undefined;
prop4 = undefined;
}
Or just define the class and use new.
I'm considering the first approach because:
- there might be some memory savings with null prototype (and smaller hashtable?)
- it is nicer to iterate over object properties without doing 'hasOwnProperty'.
- Also it will be easier for me to debug without a bunch of
undefinedproperties hanging on the list in IDE/DevTools.
At the same time, I vaguely remember that v8 was doing some JIT magic with compilation of objects, but if I continue to add properties overtime, that this optimization could go out of the window.
Which option is better from memory usage and/or performance point of view?
prop3might not be needed, not creating it is almost certainly better performance-wise. It's hard to imagine how doing something can be faster than not doing it. The difference between using an object literal and assigning properties with assignments is probably negligible, but you should benchmark to be sure.cost obj = { etcis invalid syntax so that wouldn't work at all