1

I'm testing some custom data formats to send between my server and the client's browser. I want to make sure that my data is as small as I thought it was, so I did some tests and was pretty confused by the results.

var bufArr = new ArrayBuffer(160000000);
var bufView = new Float64Array(bufArr);
for(i=0; i<20000000; i++) {
  bufView[i] = 5.632 + i
}

On the server I make an ArrayBuffer with 160,000,000 bytes. Then I fill the buffer with an "array" of 20,000,000 float64s. each float64 is 8 bytes, so 20,000,000 * 8 = 160,000,000. at least, I think that's how this works, but please correct me if my asumption is wrong.

Next, I connect to my webpage, and when I click a button, the server uses Socket.io to send me bufArr, which simply looks like this:

socket.on('test1', function(data) {
  socket.emit("test2", bufArr)
})

So in theory I should be sending about 160MB. But what I found is that it is only sending about 23MB. The client actually gets the full data, and I console log it, and look through it, and it is accurate. For example:

19989996:19990001.632
19989997:19990002.632
19989998:19990003.632
19989999:19990004.632

The number on the left is the index, so if i= 19989999, then we add 5.632 and get 19990004.6, and this is true for every index, so no data got lost or anything.

So I receive exactly 20,000,000 floating point numbers which should be 160MB, but when I check my network monitoring tools (Windows 10 settings, Task manager, and also my router traffic analysis page) and all of them say that I received about 23MB.

Is my understanding of ArrayBuffers and Float64Arrays incorrect, or is there some magic happening within socketio, or my router that is making the transmit size significantly lower?

frogengine773
  • 357
  • 3
  • 11
  • See [this answer](https://stackoverflow.com/a/45561784/4025095). – Myst Mar 21 '18 at 04:21
  • Oh... and your assumption about byte length is misguided. `socket.io` (you aren't using raw Websockets) is sending a JSON message, so the data is converted to strings and the float data is as long as the string (not the binary representation)... – Myst Mar 21 '18 at 04:23
  • Ok thank you for that article, but socketio is capable of sending binary data as a buffer. Also, if its true that it gets converted to JSON, then I should be receiving MORE than 160MB, not less. – frogengine773 Mar 21 '18 at 17:00
  • you make a good point about socketio and optional binary transport (I’m not sure how to set it up). I really don’t know your setup. Your specific testing environment *might* be compressing the data (either binary or JSON)... but most environments won’t negotiate compression. – Myst Mar 21 '18 at 17:07

0 Answers0