Edit: Thanks to @zenoh for the counting method. gzipping produces characters with a double byte count, so that was interesting. I am still interested in knowing how people are reducing the size of the data they send over WebRTC?
I want to send some JSON over the WebRTC. It is a lot of data, so I need to keep it to a minimum.
I need to calculate the actual size of the data that is being sent. I need tips and tricks to keep this to a minimum.
Say, for example I have an object:
{
myString: "Yabba Dabba Doo",
myInt: 42,
myFloat: 1.6666666666666666666666666666666666666666666...., // Recurring
}
I always had the (miscomprehension) belief that JSON put it all into a string form. So, for example the float would be written as "1.6666666666666" with loads of 6's afterwards until it got bored. That is obviously loads of bytes. That is bad.
So could someone tell me how to calculate the exact bytes size of the JSON I am sending?
Should I be making my keys as small as possible? So instead of making it readable, ie 'myfloat' as a key, make it small, ie 'f' as a key?
I have a little zip routine I have coded for ints. I think beyond the 9th decimal place, the floats become almost irrelevant for what they are needed, so I was thinking of multiplying the float by 1000000 remove the remaining decimal places, and then use my little zip routine on the int. Then do the reverse when it is sent over WebRTC.
Another idea I had was JSONifying the object, then gzip-ing the resultant string and send that.
So any ideas on the subject are gratefully received.
JSON.stringify(obj)
should output string in UTF-8, then you can calculate the size using UFT-8 string length & byte counter like this one: https://mothereff.in/byte-counter