Search code examples
jsonoptimizationsocket.iosizesend

Questions about size of Json data sent by Socket.io/Node.js?


  1. Will tab/space like the below example increases the size of data sent?
  return {
      id:self.id,
      username:self.username,
      score:self.score,
      level:self.level
  };  

vs

return {id:self.id,username:self.username score:self.score,level:self.level};

  1. Is there any size difference between 0/1 and true/false for Json?

  2. Is there a size difference between "11" (string) and 11 (double)?

The Json will be sent 10 times every second with socket.emit of Socket.io.


Solution

    1. There should be no difference in the size based on the server side object format. Any size would be type dependent and how your serializer actually converts the object properties.
    2. Is there any size difference between 0/1 and true/false for Json? There might be a dependence on the server type but number vs Boolean results would be within a string (serialized) as "mybool:true,mynumber:1" So, if you were to optomize for size "a:true,b:1"` note the NAME is smaller so the serialzed content would be.
    3. Is there a size difference between "11" (string) and 11 (double)? Similar to the second example "mysuperlongnameisgreat:"11",mysuperlongnumbername:11" vs {"a":"11","b":11}" thus the number is smaller by excluding those two quotes.

    All that being said considering the total processing (IF speed is an issue) this has to be deserialized into a JavaScript object on the client side so IF you are using the number as a number it will need to parse that at some point to the proper type and thus may be more of an impact than serialized content size.

    Note that with short names, you WILL have a negative impact on maintenance as it is much less intuitive to maintain short non-descriptive names than longer ones.

    Using your example, it would be "smaller" to do (assuming a strongly typed server side)

     return {
          d:self.id,
          n:self.username,
          s:self.score,
          v:self.level
      };