Search code examples
node.jsperformanceiosocket.ioprocessing-efficiency

Socket.IO bare-bones no-method objects or fully-equipped class-based objects for client updates?


Say I have an array of players in my Node app, each player being an object. Every tick, I send my array to my clients, with Socket.IO. I'm not sure, out of the two following methods, would be the most efficient way of doing this:

  1. For each player, I add a basic object to my array, such as {id:1,x:10,y:20,color:"#000000"}. Then I can send the whole array, raw, to my clients. Functions that I need are non-object specific and handle the parsed object. This options is used in this Agar.io clone.
  2. For each player, I add an object based on a Player class, like {id:1,x:10,y:20,color:"#000000",update:function(){//code},someOtherFunction:function(){//code}} and then create a pseudo array of pseudo objects (like the one in option 1) to send to my players each tick.

If there are any better alternatives, please outline them.


Solution

  • Data stored in your app should be optimized for best coding style. That means it should be readable, maintainable, extensible, robust, etc...

    Data sent via a transport should be optimized for most efficient transport.

    One should not really influence or drive the other. If the two separate priorities lead to different formats, then you simply convert from one to the other before sending data or when receiving data.


    Given all that, I would think that you would want to use object oriented principles within your app so that a player could be an actual object with methods.

    Then, when you want to send data across the wire, you just pull out whatever data you want to send, format it into something that is efficient for transport and send it. The receiver will then parse the data and insert it into an object.