Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
122
rated 0 times [  125] [ 3]  / answers: 1 / hits: 17760  / 13 Years ago, mon, may 2, 2011, 12:00:00

I'm writing an API for retrieving data from a JDBC-connected Java Servlet via JSON. I've chosen to use JSON because we'll want to do sorts and other operations on the data in the browser, and we'll be accessing the data from across domains.



Since I'm essentially doing SQL queries in JavaScript, the data that comes back is tabular in nature. I started to write this so that you get back a list of column labels, then arrays of values, for example:



{
columns: [
given_name,
surname,
],
results: [
[
Joe,
Schmoe
],
[
Jane,
Doe
]
]
}


But as I start to write the JavaScript to deal with the returned data, I wonder if it might be better to just output the results with key/value pairs, such as:



{
results: [
{
given_name: Joe,
surname: Schmoe
},
{
given_name: Jane,
surname : Doe
}
]
}


If you're returning a lot of results, that's a lot of repeated text. But we're going to be transporting gzipped, so I'm not too concerned about bandwidth.



Basically, should I engineer this so that I'm accessing my data with



$.getJSON(query, function(data) {
var columns = data.columns;
var results = data.results;
$.each(results, function(key, row) {
console.log(row[columns.indexOf('surname')]);
});
});


or the much prettier



$.getJSON(query, function(data) {
var results = data.results;
$.each(results, function(key, row) {
console.log(row.surname);
});
});


?



Essentially, I want to know if the potential hit to performance justifies the much cleaner syntax of the latter option.



Follow up



I did implement it both ways and profile. Profiling was a great idea! The differences in performance were marginal. The differences in data transfer size were substantial, but with Gzip compression, the variance was down to 5-6% between both formats and between very large and very small data sets. So I'm going with the prettier implementation. For this particular application, I can expect all clients to support Gzip/Deflate, so the size doesn't matter, and the computational complexity on both the client and server is similar enough that it doesn't matter.



For anyone interested, here is my data with graphs!.


More From » jquery

 Answers
22

Synthesizing other answers:




  1. Your wire format doesn't have to be the same as your in-memory format.

  2. Profile which is better - see if it makes a difference.

  3. Simpler is usually better to start with.



Further:




  • If you just have a page of results, and few users, then the 2nd format may be no worse than the 1st format.

  • If your data is quite sparse, the 2nd format may well be better.

  • If you're sending 1000's or rows of data, and you have millions of users, then it's possible that the size of data you send can start to matter, and perhaps the 1st format may help.

  • You can't guarantee that all user agents support gzip / deflate, so bear this in mind.


[#92445] Friday, April 29, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
janjadonb

Total Points: 4
Total Questions: 114
Total Answers: 118

Location: Mali
Member since Fri, Dec 3, 2021
3 Years ago
janjadonb questions
;