Performance in Javascript objects

Asked

Viewed 186 times

6

By which means are obtained the best performance of Javascript objects.

var Objeto = { 
    propriedade:'valorPropriedade',
    init:function() {
       //Inicia Objetos  
    }
}
var obj = Objeto.init();

or:

function Objeto() {
   this.propriedade = 'valorPropriedade';
   this.init = function(){
        //Função para iniciar o Objeto
   }
}

var obj = new Objeto();
obj.init();
  • 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 is obj.

  • 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.

1 answer

5


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).

  • 2

    I think the big difference when creating the object is that new Ctor involves a function call, which implies the creation of a new lexical environment etc. Regarding use, you are right, the extra level in the prototype chain must cause an overhead when accessing an inherited property or method.

  • 1

    @bfavaretto Well observed! I updated the tests isolating the creation of the call object from the init, and tests ranged from "almost equal" (in Firefox) to "Objeto much better" (in Chrome). I imagine that V8 should do some optimization in the case of "classes", I do not know... Anyway, such a trivial code is almost impossible to draw any definitive conclusion - nor does it have any relation to the performance of the application at all.

  • Really unexpected this difference in the tests you added. I expected the call to init have the same performance in both cases. I can’t imagine what kind of optimization V8 is doing!

  • Further legal tests: http://jsperf.com/construct-ou-initiator/2

  • 2

    I did not resist and asked why in the OS: http://stackoverflow.com/questions/22057579/why-is-calling-a-method-on-an-object-literal-slower-on-v8

  • @bfavaretto I read the question you asked, the answers and the comment link. Very interesting indeed, in the end the performance will be the opposite than my answer and the simple test pointed out (in V8 at least - what meets the OP, since it intends to use Node.js). If you want to post your findings as an answer, go ahead, otherwise tomorrow night I’ll update my.

  • Excellent research and very good to see these materials being built in Portuguese, I will continue following both discussions, I find very useful, mainly for the application of the concepts raised.

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.