Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance text/html vs. application/json

While evaluating performance of PHP frameworks I came across a strange problem

Sending a JSON as application/json seems to be much slower than sending with no extra header (which seems to fallback to text/html)

Example #1 (application/json)

header('Content-Type: application/json');
echo json_encode($data);

Example #2 (text/html)

echo json_encode($data);

Testing with apache bench (ab -c10 -n1000) gives me:

Example #1: 350 #/sec

Example #2: 440 #/sec

which shows that setting the extra header seems to be a little bit slower.

But:

Getting the same JSONs via "ajax" (jQuery.getJSON('url', function(j){console.log(j)});) makes the difference very big (timing as seen in Chrome Web Inspector):

Example #1: 340 ms / request

Example #2: 980 ms / request

Whats the matter of this difference?

Is there a reason to use application/json despite the performance difference?

like image 621
stwagner Avatar asked Feb 18 '13 22:02

stwagner


People also ask

Is JSON faster than plain text?

JSon is basically a format of plain text. As such it can't be faster than the best plain text format. (It could be faster than a poorly chosen plain text format) JSon is used because it makes encoding and decoding easier and is fairly human readable for many types of data, esp complex ones.

What is the difference between application JSON and text JSON?

In other words: text if it is readable text (which JSON is) application if its unreadable binary data (which JSON is not)

What is the difference between JSON and text?

Steven Parker. In that case, the difference is that ". text()" will give you the result in string format, and ". json()" will parse it from JSON and convert it into an object.

Is JSON just text?

JSON is a text-based data format following JavaScript object syntax, which was popularized by Douglas Crockford. Even though it closely resembles JavaScript object literal syntax, it can be used independently from JavaScript, and many programming environments feature the ability to read (parse) and generate JSON.


2 Answers

I'll take up the last part of question:

Is there a reason to use application/json despite the performance difference?

Answer: Yes

Why: 1) text/html can often be malformed json and will go uncaught until you try parsing it. application/json will fail and you can easily debug whenever the json is malformed

2) If you are viewing json in browser, having the header type will format it in a user-friendly formatting. text/html will show it more as a blob.

3) If you are consuming this json on your webpage, application/json will immediately be converted into js object and you may access them as obj.firstnode.childnode etc.

4) callback feature can work on application/json, but not on text/html

Note: Using gzip will sufficiently alleviate the performance problem. text/html will still be bit faster, but not the recommended way for fetching json objects

Would like to see more insight on performance though. Header length is definitely not causing performance issue. More to do with your webserver analyzing the header format.

like image 61
Vishal Verma Avatar answered Sep 20 '22 18:09

Vishal Verma


Does your server handle gzipping/deflate differently depending on content-type? Mine does. Believe ab does not accept gzip by default. (You can set this in ab with a custom header with the -H flag). But Chrome will always say it accepts gzipping.

You can use curl test to see if the files are different sizes:

 curl http://www.example.com/whatever --silent -H "Accept-Encoding: gzip,deflate" --write-out "size_download=%{size_download}\n" --output /dev/null

You can also look at the headers to see if gzipping is applied:

curl http://www.example.com/whatever -I -H "Accept-Encoding: gzip,deflate" 
like image 21
AllInOne Avatar answered Sep 23 '22 18:09

AllInOne