3

My Javascript application is downloading quite a bit of data from the server and I was thinking that in addition to normal gzip that's done by the server, I can encode the data in some binary format rather than textual JSON.
Is there a standard way of doing this?
Ideally it should be some small tool that can take a JSON text file and convert it to a generic binary format and a small Javascript library which decodes it.

Also, is there something special that needs to be done in XHR to pass binary data?

1
  • If the server is compressing the data, turning it into binary first probably won't help much. You'll find your binary version compressed will have a similar size to the text version compressed. Commented Mar 2, 2011 at 10:40

3 Answers 3

3

If gzip doesn't compress well enough, chances are your binary format won't either, especially if you wan't to be able to decode it via javascript within a reasonable amount of time.

Remember that the unzipping when using gzip is done natively by the browser and is orders of magnitude faster than anything you can do in javascript.

If you feel that the JSON deserialization is too slow, because you are supporting older browsers like ie7 which doesn't decode JSON natively but depends on eval for the job, consider going away from JSON to a custom encoding based on string splitting, which is much much faster to deserialize.

For inspiration try to read this article:

http://code.flickr.com/blog/2009/03/18/building-fast-client-side-searches/

Sign up to request clarification or add additional context in comments.

Comments

3

Check out BSON

BSON, short for Bin­ary JSON, is a bin­ary-en­coded seri­al­iz­a­tion of JSON-like doc­u­ments. Like JSON, BSON sup­ports the em­bed­ding of doc­u­ments and ar­rays with­in oth­er doc­u­ments and ar­rays. BSON also con­tains ex­ten­sions that al­low rep­res­ent­a­tion of data types that are not part of the JSON spec. For ex­ample, BSON has a Date type and a BinData type.

Find a good explanation here http://kaijaeger.com/articles/introducing-bison-binary-interchange-standard.html

5 Comments

Just a note: there are no JS implementation of BSON.
There is one I think, check this example: kaijaeger.com/downloads/bison/examples/view/bisonclient.html
You are right :-) However, as stated in the article, that implementation is not working really well (the message size is pretty big) and I do not know if it may be useful on a real-world application.
Looking at the specification for BSON, I don't think a BSON encoded document would be much smaller than a JSON encoded document. For instance, a key name in a JSON object will only be one byte longer than a key name in a BSON object. Numbers less than 1000 actually take more space than in JSON as do full UTF-8 strings, although you can use the C string format in BSON to store UTF-8 (the comment about char 0 in the spec of BSON is wrong).
This answer confuses BSON and BISON. The first link is for BSON, the standard used by MongoDB, while the second (with the JavaScript implementation) is for BISON, a pet project. (To be fair, the creator of BISON erroneously refers to it as BSON on his own site...)
1

MongoDB is using something like that for their document-oriented storage. You can get more details directly on BSON website.
Unfortunately, BSON does not work with Javascript (as you can see from the implementation list), so I think it is not a good answer to your question.

You could think about using Protocol Buffers; it has a JS encoder/decoder, but it is still quite experimental.
You may try it - lots of times, experimental open source project are already good enough for usage in specific scenarios.

Note also that there is some questioning about BSON being more compact than JSON; the same could be true also for other protocols like protbuf - I would strongly suggest you doing some math and check it out if there are actual gains.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.