0

I will use AJAX for a pagination script and wanna make this script as "data consumption saving" as possible. Therefore my idea is to just load the items for the chosen page and just load empty li-items with data-id for all items not showing.

The question is if this really is data consumption saving if the php file called by ajax is going through all the data anyway.

I guess the question is; does the data count in how much content there is in the ajax-response, or is it counted by how big the ajax-call to the php file is?

Do I save any data by leaving a lot of li-items empty or could I just as good add the content since it loaded in the ajax-call anyway?

Does anyone understand how I mean?

People always want code example:

jQuery.post(ajaxurl, 'action=load_items', function(response)
{
            // Is it the action 'action=load_items' 
            // that consumes data? Or is it the amount
            // of data coming out in the response?
});
Peter Westerlund
  • 741
  • 1
  • 10
  • 36
  • I don't understand what you mean. Do you have any real example, with code do demonstrate your question ([mcve])? – Alon Eitan Jun 04 '17 at 08:20
  • dont have empty bits... use json, and then build the dom with js from the response, that would be the "lightest" data consumption, also use gzip on the server will also reduce data consumption, though its really hard to know what you mean by data consumption. :) – Lawrence Cherone Jun 04 '17 at 08:25
  • But I need the empty bits to make the javascript knowing what page the loaded items are on. My question is like; What decides how much datas been consumed? Is it 1. depending on how heavy the php-file-load in the ajax call is? Or is it 2. depending on how much datas been coming out in the ajax response? – Peter Westerlund Jun 04 '17 at 08:40
  • This is still unclear. What is `heavy`? What is the __real problem__ behind all these? – u_mulder Jun 04 '17 at 08:48
  • I just wanna minimize the amount of data the script consumes. – Peter Westerlund Jun 04 '17 at 08:53
  • Tha ajax call does two things: 1. Trigger a php script. 2. Receives data. Does both of those things consumes data? I really don't know how to make the question more clear.... – Peter Westerlund Jun 04 '17 at 08:56

1 Answers1

1

I guess you want to minimize the data a client (who's visiting the website) consumes (e.g. because of a small data plan on a mobile phone). Is this correct?

If so, you need to think about what is actually sent from your website to the client. The PHP script echos some data, and only this data is transferred to the client. So, what you actually read in during the calculation (and hold in the server's memory) is not relevant. It is relevant what you actually put out and send to the client.

This said, you can optimize the output of your script. First, you should enable GZIP. This saves the most data and every browser understands it. In PHP, this is very easy (see detailed answer). Just add ob_start("ob_gzhandler"); before your output.

Second, you should minify the JSON you send back to the client. You can do this by just using the default function json_encode(). This will remove you all unnecessary white space in the JSON.

If you now send only data chunks instead of the whole data set, it would probably be the most data-savy way to do that. Keep in mind, that there is always a HTTP header which needs to be transmitted too. So don't make too small data packages if you want to optimize the data consumption (ideally, you send exactly what the average user wants to see in one package).

You can track the data consumption of your website in the Network tab of e.g. Google Chrome's Dev Tools (I'm sure this is also available in other browsers). Every Ajax call is listed there together with its size (you can filter it with XHR).

ssc-hrep3
  • 15,024
  • 7
  • 48
  • 87