문제

I use some rather large JSON files (really, only several hundred "records"). Is compressing said files (using the YUI Compressor) worth the hassle/time?

I get the files via $.getJSON()

도움이 되었습니까?

해결책

In most cases, it doesn't seem that it would make much sense to do that. How could the YUI compressor offer any benefit on a JSON file without breaking the code that uses that JSON data?

In our discussion in the comments, you mentioned removing whitespace from the JSON file. If you have a hand-edited JSON file with indentation and whitespace and all that, then the compressor could remove that. But most JSON is machine-generated with a call to a function like to_json() or json.dumps() or whatever your server language uses. Typically, unless you specify an option that says you want pretty-printed output, the JSON will be generated without extra whitespace.

Don't forget that your browser and server are almost certainly providing gzip compression (but double-check to make sure that's working properly). To find out the actual size of the data transferred over the wire, gzip your JSON file and see how small it is in that form. Either use the gzip command on Linux or OS X, or on Windows you can use 7-Zip to create a .gz file from your JSON.

I think you may be pleasantly surprised by how small the gzipped version is for a typical JSON file. A lot of the bulk in a JSON file tends to be the repeated property names, and those compress right out.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top