views:

675

answers:

2

I'm passing a table of up to 1000 rows, consisting of name, ID, latitude and longitude values, to the client.

The list will then be processed by Javascript and converted to markers on a Google map.

I initially planned to do this with JSON, as I want the code to be readable and easy to deal with, and because we may be adding more structure to it over time.

However, my colleague suggested passing it down as a Javascript array, as it would reduce the size greatly.

This made me think, maybe JSON is a bit redundant. After all, for each row defined, the name of each field is also being outputted repetitively. Whereas, for an array, the position of the cells is used to indicate the field.

However, would there really be a performance improvement by using an array?

The site uses GZIP compression. Is this compression effective enough to take care of any redundancy found in a JSON string?

[edit]

I realize JSON is just a notation.

But my real question is - what notation is best, performance-wise?

If I use fully named attributes, then I can have code like this:

var x = resultset.rows[0].name;

Whereas if I don't, it will look less readable, like so:

var x = resultset.rows[0][2];

My question is - would the sacrifice in code readability be worth it for the performance gains? Or not?


Further notes:

According to Wikipedia, the Deflate compression algorithm (used by gzip) performs 'Duplicate string elimination'. http://en.wikipedia.org/wiki/DEFLATE#Duplicate%5Fstring%5Felimination

If this is correct, I have no reason to be concerned about any redundancy in JSON, as it's already been taken care of.

+7  A: 

Hi,

JSON is just a notation (Javascript Object Notation), and includes JS arrays -- even if there is the word "object" in its name.

See its grammar on http://json.org/ which defines an array like this (quoting) :

An array is an ordered collection of values. An array begins with [ (left bracket) and ends with ] (right bracket). Values are separated by , (comma).

This means this (taken from JSON Data Set Sample) would be valid JSON :

[ 100, 500, 300, 200, 400 ]

Even if it doesn't include nor declare nor whatever any object at all.


In your case, I suppose you could use some array, storing data by position, and not by name.

If you are worried about size you could want to "compress" that data on the server side by yourself, and de-compress it on the client side -- but I wouldn't do that : it would mean you'd need more processing time/power on the client side...

I'd rather go with gzipping of the page that contains the data : you'll have nothing to do, it's fully automatic, and it works just fine -- and the difference in size will probably not be noticeable.

Pascal MARTIN
Note that JSON is just a subset of JavaScript.
Gumbo
And a JavaScript array is just a subset of JSON. What you really want to know is whether to store the data as arrays or objects in JSON.
Marius
@Marius: No. JSON is a subset of JavaScript and *not* vice versa. This is valid JavaScript but not valid JSON: `[foobar]`.
Gumbo
+4  A: 

I suggest to use a simple CSV format. There is a nice article on the Flickr Development Blog where they talked about their experience with such a problem. But the best would be to try it on your own.

Gumbo