0

I'm using a PHP script that returns a JSON array that's requested from an AJAX script.

My problem is with the the size of this array, since it must show more than 10,000 items, which are an associative array. The problem is the time that the web browser employs reading the JSON:

$.ajax({
    url: "urlto/product.php",
    dataType: 'json',
    timeout: 800000,
}).done(function (o) {
    for (var i = 0; i < o.length; i++)
    {
         // do something with o.[i]....
    } 
});

How can I decrease the request time? How can decrease the time of loading? Specifically, not the time of the PHP execution, but the load time of the AJAX and the web browser. Thanks.

Cyril Graze
  • 3,881
  • 2
  • 21
  • 27
  • 1
    Since you don't want to speed up the actual ajax request (php), you are actually asking to speed up your loop. Check : https://stackoverflow.com/questions/5349425/whats-the-fastest-way-to-loop-through-an-array-in-javascript . But if I were you, I would ask myself why you would want to load 10k items at once, split this up... – Zunderscore Jul 28 '17 at 19:50
  • no one is going to see 10000 items at a time, u must use pagination for it , also use search button . Just see Datatable plugin of jquery may be it helps ur problem in another way – dharmx Jul 29 '17 at 00:47
  • to answare to Zunderscore, i user mysqli in php then i use a cycle while ($row = $result->fetch_assoc()) {...} are there any way to improve performace in this case? it's better this or what you post? – Filippo Luppi Jul 29 '17 at 02:25
  • to answare dharmx, i use datatable to paginate the table and show some results of whole result, but i still get all result, how can i get result dynamically? and the research will search only in the present rows on all that are not shows? – Filippo Luppi Jul 29 '17 at 02:27

2 Answers2

0

Instead of using a for-loop try to use Array.prototype.map. https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/map Maybe you're gaining some performance against the loop.

Auskennfuchs
  • 1,577
  • 9
  • 18
  • you can replace the for-loop with o.map(function(curElement){ //do the same as with o[i] but with curElement }); – Auskennfuchs Jul 29 '17 at 08:08
  • But as you clarified in the comment of your question you want to paginate the result in any case. For this you have to advance your backend to take some more parameters, e.g. `url: "urlto/product.php?page=3&items=20"`. With these parameters you can add something like this to your query `fetch 20 rows only starting with 3*20` (<- no real SQL). With the result you can replace the whole content of your datatable in the frontend. – Auskennfuchs Jul 29 '17 at 08:22
  • Do the same thing with sorting and searching and you can use the whole dataset but from backendsite. Also your DB and your backend will be happy if they don't have to provide the whole dataset everytime ;-) – Auskennfuchs Jul 29 '17 at 08:22
-1

This is something that is usually solved using a "pagination" method.

You define an interval (say 50 records) and only query for the active view records, then display for the documents within that rage.

By doing this, you should be capable of reducing your browser's cpu/memory overhead requirement for your page.

I am at a lack of free time to go into a contextual example, but if time allows and another answer doesn't come in I will elaborate.

Good luck!

  • yes i know, paginatio will be a good way but i can't beacouse i need to search on whole 10000 rows..... i show these in a html table formatted with datatable – Filippo Luppi Jul 29 '17 at 02:29