Question

My goal is to serialize JavaScript native datatypes for storage into an SQLite database. I'm doing this within a Firefox extension, and so I have access to the Mozilla platform XPCOM api. Initially I was thinking that the easiest thing to do would be to just store JSON strings.

However, if there is a way to serialize native datatypes within the Mozilla platform, I'd like to know where I can find it. And if this is possible, what storage space savings can I expect for the extra work (if any) and would there be a performance cost vs JSON?

Of course, the gains of storing data in a binary format may not outweigh the ease of simply using JSON. Any suggestions?

Was it helpful?

Solution

Firefox 3.5+ has native JSON encoding/decoding, which is faster than Javascript-engine decoding of JSON, according to Wikipedia.

Given the implication of rapid (de)serialization in FF3.5+, I'd say that you may as well stick with JSON strings. That way will also be virtually cross-platform compatible if you ever need to inspect the DB from another language/platform.

As far as string overhead goes, if you strip out unnecessary whitespace, you're looking at an overhead of a few braces, a few quotation marks, a few commas/colons, and the cost of storing a number as a string instead of a native format. Unless that proves to be a bottleneck in your application, the added simplicity of using simple JSON strings seems worth it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top