1

I am sending the following data on the server.

        var loc = {
            type            : 'exloc',                      
            ms              : this.var1.move,
            ppx             : this.var1.ppx,
            ppy             : this.var1.ppy,
            Name            : "red",
            rid             : this.gv.GlobalInfoHolder.CurrentRoomID
        };

and I can send this data in the following form also.

 var obj = "type=exloc,ms="+this.var1.move+",ppx="+this.var1.ppx+",ppy="+this.var1.ppy+",Name=red,rid="+this.gv.GlobalInfoHolder.CurrentRoomID;

Then I can strip the data based on the comma's I've put.

Another way would be to encode the object to json then send the data to server and decode.

So I wanted to ask that what will be the best solution for this? It would be better if anyone tells me how to analyze the size in bytes of each of these variables including the json solution also.

I want to go for the solution that gives me the smallest size of the variable.I am doing this for performance and efficiency if anyone wants to suggest something he/she is most welcome.I am considering this because I am working on a real time game which has to share data between client and server very quickly (in milliseconds)

Mj1992
  • 3,124
  • 10
  • 54
  • 97
  • JSON is very little overhead for what you are attempting to do, and accounts for all types of characters that may end in your input data. Of course, your comma separation method will be less overhead; but I have used JSON via AJAX in extremely heavy traffic environments with no issues whatsoever. There is a reason that JSON is basically the standard in plain text object transmission between systems. – Dave Lasley Feb 17 '13 at 00:03
  • @Dave - JSON is great for interoperability. If you were sending 1000 records of data each with the same fields in it, JSON would be full of redundancy and other formats would be far, far more efficient. – jfriend00 Feb 17 '13 at 00:06
  • But what if we are talking about data sharing between thousands of users in milliseconds, would these solutions be still enough ? – Mj1992 Feb 17 '13 at 00:17

1 Answers1

1

Just get a network analyzer program (like the free Fiddler or many, many other programs available for the same thing) and look to see exactly what is being sent across the wire. I do this all the time to debug client/server problems. It's real easy and shows you exactly what is being sent between client and server.

JSON is not meant to be the most storage efficient format so if you're trying to optimize for size, you can already know that's probably not the best option because formats with less redundancy (single copy of field name vs. many) are nearly always going to be more compact.

In addition, you should first figure out whether any of this matters. If your data is small overall, then the performance difference between 40 bytes and 80 bytes is probably too small to even measure on a single client. On the other hand, if you had thousands of records to transmit, then the size of each record could be enough to make a difference.

jfriend00
  • 580,699
  • 78
  • 809
  • 825
  • I want to send the data I've shown you in the question, I used the function this guy has suggested `http://stackoverflow.com/questions/1248302/javascript-object-size` to find the object byte size so it was giving the size around 100-104 Bytes from this function.I want to send the data more frequently because I am working on a real time game so that's why I am considering this – Mj1992 Feb 17 '13 at 00:08
  • If that's the only data and you're only sending one record of this data at a time, then it simply doesn't matter what format you use because the data is so small. Use whatever format is easiest for you on client and server. If it was an API consumed by other coders, I'd say use JSON because it's fully documented, fully interoperable and requires no documentation. I'd be surprised if you could tell the performance difference between 20 bytes and 30 bytes. It probably all goes in one packet and there's more TCP signaling overhead than your actual data. – jfriend00 Feb 17 '13 at 00:13
  • If its a case of data sharing in milliseconds between thousands of users would this be ok ? – Mj1992 Feb 17 '13 at 00:15
  • @Mj1992 - If your data us less than a typical TCP packet, it's not going to matter. Read [this post](http://superuser.com/questions/243008/whats-the-minimum-size-of-a-tcp-packet) for an idea of the overhead involved in TCP/IP. Your going to have at least 40-50 bytes of TCP/IP overhead (probably even more) for a single packet transmission and that doesn't even count the confirmation packets that are sent to confirm delivery. – jfriend00 Feb 17 '13 at 00:18
  • @Mj1992 - Also keep in mind that this should be trivial to change once you have your app up and running if you ever get any info that it needs to be smaller. Premature optimization is usually wasted time making things more complicated than they need to be and taking time that is better spent on things that matter more. – jfriend00 Feb 17 '13 at 00:19