0

I'm just wondering if it's worth it, I'm using nodejs with socket.io and I need to send medium sized arrays to clients which contains small strings and numbers. Would it be worth it to zip them or something or would the time to compress them would defeat it's own purpose to be faster ? The array I'm trying to compress are less that 1 mb.

As of now I see no latency but who knows, someone might have slow internet or old devices.

no_one
  • 19
  • 7

1 Answers1

2

It depends entirely upon how large the arrays are and how much they would benefit from compression - neither of which you have disclosed.

For example, if they were 50k and could be compressed to 40k, that difference would be unlikely to be perceived.

If they were 1MB and could be compressed to 300k, that difference could be meaningful.

You will need to measure how large they typically are and then, if those are in a range where it might make a meaningful difference to compress them, then do some tests on how much they compress.

FYI, you can also look at how exactly the data is being sent over the wire because socket.io's default of JSON is not always the most compact way to format things either. For example, sending a large array of objects is going to repeat property names over and over in the JSON which might benefit a lot from compression, but might benefit even more from using a custom data format that's more compact.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • And how would I get the size of an array ? The only questions on this I can find all says to use .length but that's not what I'm looking for, an array's .length is just the number of index which contains more stuff. – no_one Feb 28 '15 at 17:48
  • @LordDoomer - You can just look in the network tab of the Chrome debugger, go to the webSockets section and see how big your webSocket messages are. You may have to open this section of the debugger before you start sending the packets so it can start recording. – jfriend00 Feb 28 '15 at 17:49
  • I guess I could make a for loop and add the length of all items and sub items from sub arrays but that's kind of ugly... – no_one Feb 28 '15 at 17:50
  • WebSockets have a [Compression Extension](https://tools.ietf.org/html/draft-ietf-hybi-permessage-compression-21) (also http://stackoverflow.com/questions/19298651). Unless there was some domain-localized compression that could be used I would look no further. – user2864740 May 23 '15 at 20:09