Z
Z
ZeroSkill2021-05-17 16:06:48
JavaScript
ZeroSkill, 2021-05-17 16:06:48

How can I optimize the constant transmission of large strings consisting of decimal numbers (websocket)?

Hello, I am sending server-client BIG strings like "0 2132 4636 33 1 1917 4267 22 2 1878 4060 36" , where the number of numbers is a multiple of 4, over a websocket connection. How can I compress or something else to optimize the transmission of these lines. I read about typed arrays (binary), but I can’t figure it out at all. Can they somehow be able to translate and transmit everything?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly Kachan, 2021-05-17
@MANAB

The string in your case is an array of char, where char takes 1 or 2 bytes, depending on the encoding method. int takes 4 bytes. If your numbers, for example, do not go beyond the range of 16384, i.e. 16 bits (2 bytes), you can save money by packing them in binary form. Xs how to do this in javascript, in C# through bitwise shifts for example. https://stackoverflow.com/questions/5930166/packin...
In this case, your string "0 2132 4636 33 1 1917 4267 22 2 1878 4060 36" instead of 44(88) bytes will take 24 bytes, because 12 numbers of 2 bytes and separators are no longer needed.

R
Roman Mirilaczvili, 2021-05-17
@2ord

Work with an array of numbers, not a string.
Pack into a byte array and apply compression. Gzip, Deflate, or something more specialized, depending on the nature of the data.
Take at least MessagePack or do it yourself:
https://developer.mozilla.org/ru/docs/Web/JavaScri...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question