stwagner
stwagner

Reputation: 267

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?

Upvotes: 18

Views: 5886

Answers (2)

Vishal Verma
Vishal Verma

Reputation: 982

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.

Upvotes: 5

AllInOne
AllInOne

Reputation: 1450

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" 

Upvotes: 3

Related Questions