Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a large dataset to the client - Javascript arrays or JSON?

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_string_elimination

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

like image 745
Jonathan Avatar asked Dec 14 '22 02:12

Jonathan


1 Answers

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.

like image 195
Pascal MARTIN Avatar answered May 30 '23 18:05

Pascal MARTIN