JSON vs HTML through PHP

Hey,

I’m wondering which would be faster to retrieve the data as JSON and let javascript sort it out for me or just store the HTML in a variable and let PHP echo it?

For example I currently something like this


$.ajax({
  type: 'POST',
  data: 'id=' + id,
  url: 'page.php',
  success: function (html) {
    $('.section').html(html);
  }
});

But if I did it through JSON would it like so:


$.ajax({
  type: 'POST',
  url: 'page.php',
  data: 'id=' + id,
  dataType: 'json',
  success: function(d) {
  var html = '';
  for (i=0;i<=d.length;i++) {
    html += '<div class="1">'+d[i].id+'</div> <div class="2">'+d[i].name+'</div> <div class="3">'+d[i].ph+'</div>';
    }
  }
  $('.section').html(html)
});

Would that give me a performance boost? Or is it just another one of doing things?

thanks for any input!

I suspect you’ll find that caching your HTML results and displaying them will be much faster, but is it really worth the speed bost?

The difference will be pretty minimal, and presenting the data in JSON format makes it more accessible to people who want to do things with your data. (ie. When you decide you want to animate it in 6 months time, or whatever, you can do things very easily with JSON!)

What’s more important to you - speed, or ‘programmability’ of the data?

Hhmm I see what you mean.

For this purpose I would preffer speed as the data I’m out putting isn’t available to the public or any third party.

I guess if I was creating some sort of an API it would be best to use json/XML etc to allow a developer to use the data however they liked.