6

I need to retrieve a large amount of data (coordinates plus an extra value) via AJAX. The format of the data is:

-72.781;;6,-68.811;;8

Note two different delimiters are being used: ;; and ,.

Shall I just return a delimited string and use String.split() (twice) or is it better to return a JSON string and use JSON.parse() to unpack my data? What is the worst and the best from each method?

Danziger
  • 19,628
  • 4
  • 53
  • 83
Amar
  • 11,930
  • 5
  • 50
  • 73
  • 1
    If you make the ajax call with an expected return result type of JSON, you don't need to use JSON.parse() anymore.. – Luceos Mar 25 '13 at 13:10
  • This question is very broad. You should run some benchmarks on a realistic datasets and see which one performs better. – Brandan Mar 25 '13 at 13:10
  • That's really for you to decide, isn't it? Naturally, `split` forces you to use a simple array structure in your data whereas JSON allows your data to be more complex. – Andy E Mar 25 '13 at 13:10
  • 2
    @Brandan The question seems to be more about best practices rather than which performs the best. Even if returning a single string and calling `.split()` was faster that doesn't mean it's the best approach. – Anthony Grist Mar 25 '13 at 13:12
  • `JSON.parse()` will be a better approach but you have to take care of cross browser compatibility... – Pranav Mar 25 '13 at 13:12

2 Answers2

7

Even if the data is really quite large, the odds of their being a performance difference noticeable in the real world are quite low (data transfer time will trump the decoding time). So barring a real-world performance problem, it's best to focus on what's best from a code clarity viewpoint.

If the data is homogenous (you deal with each coordinate largely the same way), then there's nothing wrong with the String#split approach.

If you need to refer to the coordinates individually in your code, there would be an argument for assigning them proper names, which would suggest using JSON. I tend to lean toward clarity, so I would probably lean toward JSON.

Another thing to consider is size on the wire. If you only need to support nice fat network connections, it probably doesn't matter, but because JSON keys are reiterated for each object, the size could be markedly larger. That might argue for compressed JSON.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • actually there is one more field value is attached with each coordinate. Let me edit the question. – Amar Mar 25 '13 at 13:14
  • Kindly see the edit. Do you still feel going for Split will be better? – Amar Mar 25 '13 at 13:18
  • 1
    @Amar: I'm not sure I said I did think `String#split` would be better (I suggested reasons you might consider each approach, I didn't advocate one). While the data you've added to the question is still pretty straight-forward to get via `String#split`, I'd be leaning toward JSON at that point, if individual parts of the data have different meanings. Real names are useful. The downside of using JSON is that it will be larger on the wire. – T.J. Crowder Mar 25 '13 at 13:18
  • Ah, so which one to go for here ? – Amar Mar 25 '13 at 13:22
  • I still feel going for split only would be better in this case. – Amar Mar 25 '13 at 13:23
  • @Amar: :-) It was always your call (and with all the facts, there's no saying I wouldn't have gone the same way; `JSON.parse` is a big hammer). Glad this helped. Best, – T.J. Crowder Mar 25 '13 at 13:31
2

I've created a performance test that describes your issue.
Although it depends on the browser implementation, in many cases -as the results show- split would be much faster, because JSON.parse does a lot of other things in the background, but you would need the data served for easy parsing: in the test I've added a case where you use split (among replace) in order to parse an already formatted json array and, the result speaks for itself.

All in all, I wouldn't go with a script that's a few miliseconds faster but n seconds harder to read and maintain.

gion_13
  • 41,171
  • 10
  • 96
  • 108
  • http://jsperf.com/json-parse-vs-string-split : this suggests the opposite, any reason? – Amar Mar 25 '13 at 13:36
  • 1
    As the response gets more complex (like arrays of arrays) both parsing methods *have more work to do*. In this example, `JSON.parse` doesn't have to do almost any validation, because all the array members are simple strings. Since you had an array of coordinates, I think that the test that I described is closer to your problem than the other. – gion_13 Mar 25 '13 at 13:42