The first option for sure. When you create a constructor function and create an object through new
, you are creating a chain of prototypes. That is, every object of the type Objeto
owns a property [[prototype]]
(is not visible from Javascript code, only indirectly via Object.getPrototypeOf(obj)
) which, in this case, is a Object
common (because you have not explicitly assigned the Objeto.prototype
).
Objeto --> Object --> Object.prototype --> null
When you don’t have a class Objeto
, only a literal, its object is of the simple type Object
, so that long string does not exist.
Object --> Object.prototype --> null
This reduces all operations that Javascript has to do on its object in 1 level. In practice, it shouldn’t make much difference, but if you want to squeeze every CPU cycle (I think it’s silly, in my opinion, but it’s your choice) then this option will be faster. Note: in the test that Linkei above the performance is extremely different, but it is because the code does nothing useful. As the program grows, this difference must become smaller, or even negligible.
Updating: as pointed out by @bfavaretto, the difference in performance is mostly the extra call from the constructor. If your code will do this frequently (create new objects) then the solution that does not use constructor should be faster. However, little can be said about the use of objects created in one way or another (at least not from this test).
Object creation or use performance? And why are you worried about this, are you having performance issues? Note also that the codes are not equivalent: what in the first is
Objeto
, in the second isobj
.– bfavaretto
I’ll edit the question code. I am developing a Single Page Application, the server side has a Nodejs and the consumption of some very heavy things. Precise speed, user-perceptible at all tips.
– flpms