My JavaScript book is out! Don't miss the opportunity to upgrade your beginner or average dev skills.

Wednesday, August 17, 2011

JSONH And Hybrid JS Objects

I have already described JSONH and now I also have the proof that it's as safe as native JSON is but on average 2X faster than native JSON operations with both small (10 objects), medium (100 objects), and massive (5000 objects and not a real world case, just a stress test to see how much JSONH scales) homogenous collections.
Wherever it's not faster it's just "as fast" but the best part is that it seems to be always faster on slower machines ( mobile ).
Moreover, the 5000 objects stress example shows that JSONH.stringify() produces a string with 54% of original JSON.stringify() size so here the summary: JSONH is faster on both compression and decompression plus it produces smaller output

yeah but ... what About Hybrid Objects

To start with, if you don't recognize/understand what is an homogenous collection and ask me: "what about nested objects?", all I can do is to point you out that Peter Michaux explained this years before me.
Have a look there and please come back after the "aaaaahh, got it: right!"

Hybrid Objects

Nowadays JSON is used everywhere but not everywhere with homogeneous collections. A simple example to screw up JSONH possibility is an object like this:

// result of a RESTful service, Ajax, query
// once again about generic articles: book!
var result = {
category: "books",
subcategory: "fantasy",
description: [
title: "The Lord Of The Rings",
description: "Learn about the darkness"
}, {
title: "The Holy Bible",
description: "Learn about both light and darkness"
// all other results out of this list

If we receive an object with one or more properties containing an homogeneous collection, as is description in above example, we may already decide to use JSONH advantages.

JSONH On Hybrid Objects

It's that easy!

// before we send/store/write data on output
result.description = JSONH.pack(result.description);

If the client is aware about the fact one or more specific property is an homogeneous collection, to obtain the original object we can do this:

// stringifiedResult as XHR responseText
var obj = JSON.parse(stringifiedResult);
obj.description = JSONH.unpack(obj.description);

// or simply via JSONP callback
data.description = JSONH.unpack(data.description);

For the same reason JSONH is faster than JSON, this operation will grant us less bandwidth to both send or receive objects, and faster conversion performances.

As Summary

I am willing to think soon about a possible schema able to describe homogeneous collections properties out of an object ... a sort of JSONH "mapper" to automate the procedure on both server side and client side and any suggestion will be more than welcome.
At least so far we know already how to adopt this solution :)


David said...

What about gzipped data transfers? My thought is that gzipped JSON is already compressed for minimal bandwidth consumption, but it would be interesting to know how JSONH performs compared with gzip in mind.

Andrea Giammarchi said...

@David, as explained in the other post gzip does not come for free.

Compress data on the fly for each user and for each request can stress the server quite a lot.

In any case, gzipped JSON size is almost the same of gzipped JSONH but in first case the browser has to unzip into a bigger JSON string and after perform a slower JSON.parse.

On server side the gzip will be also performed over a bigger JSON size.

With JSONH regardless gzip compression, stringify and parse are faster so here the sample:

JSON.stringify (10ms) + gzip (5ms) = 15ms
unzip (5ms) + JSON.parse (8ms) = 13ms

JSONH.stringify (7ms) + gzip (3ms) = 10ms
unzip (3ms) + JSON.parse (6ms) = 9ms

In any case I was planning to do a real world example test over the network and provide timing screenshots so, stay tuned ;)

entropyhacker said...

RJSON extracts data schemes on the way and uses implicit index of JSON schemes that makes it possible to compress Hybrid JS objects with any structure.

Unknown said...

JSONH saved my project!
I am dealing with a JSON object from a database result with 70,000 records. The data is pretty big, so I needed a way to store it so I don't break my server retrieving it every time. Compression is amazing and now I can store the data client side.

Thank you!