JSON response objects: cute keys and a larger response or short keys and a smaller response?

My real-time web application makes ajax requests to get responses to JSON requests.

The returned data is usually an array of objects.

Since the lot element is often present in the array (although the data is transmitted by the gzipped server) in order to keep the response size to a minimum, I keep the keys in the response very short.

For example, instead of using the description: I use d: instead of using width:. I use w: etc.

This reduces the size of the response, but on the client side, very short keys that do not contain a person make JavaScript code (which refers to the object) less readable.

The only solution seems to repeat the answer and rebuild the object with cute keys or replace them in the original received object. But this can damage the performance of the JavaScript code, resulting in a longer delay ...

Is there a better solution?


EDIT:

As BjΓΆrn Roberg suggested in his comment, I made a comparison:

pretty-response.json 459,809 bytes short-response.json 245,881 bytes pretty-response.json.zip 28,635 bytes short-response.json.zip 26,388 bytes 

Since the response is compressed by the server, the difference is really minimal.

However, a fairly response request requires the server to compress 450 KB of data, and a short response only 240 KB.

Does this affect server performance (or is there a way to measure it)?

+6
source share
5 answers

Since you plan to convert the short keys back to long keys on the client side, you are clearly concerned about the bandwidth requirements for the data, and not the memory requirements on the client.

I created several files containing random data and three keys (description, something and something else). I also dumped data through sed to replace these keys with d, s and e.

This leads to:

 750K long-keys 457K short-keys 

HTTP has compression support , and all important clients support this with gzip. So what happens if we gzip files:

 187K 10:26 long-keys.gz 179K 10:27 short-keys.gz 

There is very little choice between them, since gzip is pretty good at compressing duplicate lines.

So, just use HTTP compression and don't worry about poaching data.

gzip is also a very fast algorithm, so the impact it has on server performance is negligible.

+4
source

Perhaps you could try protocol buffers and see if it doesn't matter. It was developed faster and easier than many other serialization formats (such as XML and JSON).

There are other formats that use the same goals, but protocol buffers, such as protobuffs, are the ones that I got.

Check out this answer for a good comparison.

+1
source

You can use the decorator pattern to wrap objects after retrieving from an array.

However, given that you probably want to access all returned objects (why are you returning objects that the client does not need?), It would probably not be slower and, possibly faster, just convert the objects to objects with longer field names when retrieving from an array.

If you intend to retrieve each object several times, you can even go through the array and replace them one by one to avoid converting them again.

All these parameters have a cost of execution, but may be insignificant. Profile!

0
source

compress your json from the server http://dean.edwards.name/packer/

compression library http://dean.edwards.name/download/#packer

you can also check your json size by checking online if it will reduce or not

0
source

If you want your code to be readable and still use shortcuts, you can use index notation to access members:

 var descriptionKey = 'd'; var widthKey = 'w'; //... var description = yourObject[descriptionKey]; 
0
source

Source: https://habr.com/ru/post/958191/


All Articles