admin管理员组

文章数量:1287990

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?

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?

Share Improve this question edited May 29, 2015 at 1:18 Danziger 21.2k6 gold badges58 silver badges88 bronze badges asked Mar 25, 2013 at 13:08 AmarAmar 12k5 gold badges52 silver badges73 bronze badges 5
  • 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 Commented Mar 25, 2013 at 13:10
  • This question is very broad. You should run some benchmarks on a realistic datasets and see which one performs better. – Brandan Commented Mar 25, 2013 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 plex. – Andy E Commented Mar 25, 2013 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 Commented Mar 25, 2013 at 13:12
  • JSON.parse() will be a better approach but you have to take care of cross browser patibility... – Pranav Commented Mar 25, 2013 at 13:12
Add a ment  | 

2 Answers 2

Reset to default 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 pressed JSON.

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 milliseconds faster but n seconds harder to read and maintain.

本文标签: Splitting Delimited String vs JSON Parsing Efficiency in JavaScriptStack Overflow