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);

print(JSON.stringify(result));



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 :)

No comments:

Post a Comment