压缩 Json Javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11473478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compressed Json Javascript
提问by rayray
Currently I send JSON from an Ajax post to the server which is then converted to objects using the Hymanson Mapper.
目前,我将 JSON 从 Ajax 帖子发送到服务器,然后使用 Hymanson Mapper 将其转换为对象。
The format is like this
格式是这样的
{"id":"780710","folderID":"42024","displayOrder":2},{"id":"780724","folderID":"42024","displayOrder":3}
What is the best JavaScript library to compress this data and will the Hymanson mapper be able to handle the new format?
压缩此数据的最佳 JavaScript 库是什么,Hymanson 映射器是否能够处理新格式?
采纳答案by Beat Richartz
As said by @JamWaffles, this is the best JSON is able to do concerning compression. And in your case (the line of code you delivered), compressing further may be overkill.
正如@JamWaffles 所说,这是关于压缩的最佳 JSON。而在您的情况下(您提供的代码行),进一步压缩可能是矫枉过正。
But if you have larger responses, and you want to save those bytes, have a look at
但是如果你有更大的响应,并且你想保存这些字节,看看
or
或者
- 消息包。
They are notJSON, but they serialize data to a smaller format (in most cases).
它们不是JSON,但它们将数据序列化为较小的格式(在大多数情况下)。
回答by StaxMan
Why not just enable gzip compression that browser and web servers support? This will very nicely compress data sizes, with very little explicit work.
为什么不启用浏览器和 Web 服务器支持的 gzip 压缩?这将非常好地压缩数据大小,而无需显式工作。
回答by Mark
You can use e.g. jsonh, successor of hpack which has benchmarks on web-resource-optimization. It helps, but the same site will also show you that gzip alone will probably be enough.
您可以使用例如jsonh,hpack 的后继者,它在web-resource-optimization上有基准。它有帮助,但同一个站点也会向您展示仅 gzip 可能就足够了。
So to be clear, gzip works better than hpack, but combining them adds a little more compression.
所以要明确的是,gzip 比 hpack 工作得更好,但将它们结合起来会增加一点压缩。
回答by Gerardlamo
JsonZipperis also Awesome for multiple similar repeated objects. It allows you to use an object in its' zippedstate by only extracting one object by index from the array, so memory wise it is great as it will always only have extracted what you want.
对于多个类似的重复对象,JsonZipper也很棒。它允许您仅通过从数组中按索引提取一个对象来使用处于“压缩”状态的对象,因此在内存方面它很棒,因为它始终只提取您想要的内容。
Oh and you can actually compress on the go, so basically as you are generating data objects, you can compress them, leaving you to always have a small memory footprint.
哦,您实际上可以随时随地进行压缩,因此基本上在生成数据对象时,您可以压缩它们,从而始终保持较小的内存占用。
Most other compression algorithms have to compress&extract all the data at once.
大多数其他压缩算法必须一次压缩和提取所有数据。
Note however: if your data is a Homogeneous Collection (Exactly Same keys then hpack will be better.)
但是请注意:如果您的数据是同构集合(完全相同的键,那么 hpack 会更好。)
回答by Cyan
According to this tweet by modec, compressing JSON is indeed possible, and provides better result than tested alternatives.
根据modec 的这条推文,压缩 JSON 确实是可能的,并且比经过测试的替代方案提供了更好的结果。
It's possible to handle JSON format with nodejs, and a recent open-source project just implemented a very fast compression algorithm for nodejs.
可以使用 nodejs 处理 JSON 格式,最近的一个开源项目刚刚为 nodejs实现了一个非常快速的压缩算法。