JSON响应对象:“漂亮”键和更大的响应或短键和更小的响应? [英] JSON response objects: "pretty" keys and larger response or short keys and smaller response?

查看:102
本文介绍了JSON响应对象:“漂亮”键和更大的响应或短键和更小的响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的实时网络应用程序发出ajax请求以获取JSON经济数据响应。

My real-time web app makes ajax requests to obtain JSON econded data responses.

返回的数据通常采用对象数组的形式。

Returned data is usually in the form of an array of objects.

由于数组通常有一个很多元素(虽然发送的数据是由服务器gzip化的),以便将响应大小保持在最小值I'将响应中的设置得非常短。

As the array has often a lot of elements (an although data sent is gzipped by the server) in order to keep the response size at minimum I'm keeping the keys in the response very short.

例如,不使用 description:,我使用 d:,而不是使用宽度:我使用 w:等等......

For example instead of using description: I use d:, instead of using width: I use w: and so on...

这样做会减小响应的大小,但是在客户端,非常短的非人类可读密钥会使JavaScript代码(访问对象)的可读性降低。

Doing so reduces the size of the response but, on the client side, very-short non human-readabe keys makes the JavaScript code (that access the object) less readable.

唯一的解决方案似乎是重新解析响应并使用漂亮的键重建对象,或者在收到的原始对象中替换它们。但这可能会损害JavaScript代码的性能,从而导致更多延迟...

The only solution seem to reparse the response and rebuild the object with pretty keys or replace them in the original object received. But this may hurt the JavaScript code performance resulting in more delay...

是否存在更好的解决方案?

编辑:

BjörnRoberg在他的评论中建议我做了一个比较:

As Björn Roberg suggested in his comment I've 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

因此,当服务器压缩响应时,差异实际上是微不足道的。

So as the response is compressed by the server the difference is really minimal.

但是,漂亮的响应需要服务器压缩450 KB的数据,而短响应仅240 KB。

Still, pretty response require the server to compress 450 KB of data, while short response just 240 KB.

这是否会影响服务器性能(或者有没有办法测量它)?

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

推荐答案

由于您正在考虑将短按键转换回客户端的长按键,因此您显然很自负满足数据的带宽需求而不是客户端的内存要求。

Since you are considering converting the short keys back to long keys on the client side, you are clearly concerned with the bandwidth requirements for the data and not the memory requirements on the client.

我生成了一些包含随机数据和三个密钥的文件(描述,东西和东西) )。我还通过sed转储数据,用d,s和e替换那些密钥。

I've generated some files containing random data and three keys (description, something and somethingElse). I've also dumped the data through sed to replace those keys with d, s and e.

这导致:

750K   long-keys
457K   short-keys

HTTP有支持压缩,所有重要客户都使用gzip支持此功能。那么,如果我们gzip文件会发生什么:

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

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

他们之间几乎没有什么选择,因为gzip相当擅长压缩重复的字符串。

There is very little to choose between them, since gzip is rather good at compressing repeated strings.

所以,只需使用HTTP压缩,不要担心重复数据。

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

gzip也是一个非常快的算法,因此它对服务器性能的影响可以忽略不计。

gzip is also a really fast algorithm, so the impact it will have on server performance is negligible.

这篇关于JSON响应对象:“漂亮”键和更大的响应或短键和更小的响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆