开发者

Why do some AJAX calls take drastically longer to finish than others when the URLs have the same response time?

开发者 https://www.devze.com 2023-03-15 00:04 出处:网络
To simplify my situation; I have 3 PHP pages. Two of them adds and removes rows from a database, respectively. The third fetches all of the rows, encodes them as JSON and prints the result.

To simplify my situation; I have 3 PHP pages. Two of them adds and removes rows from a database, respectively. The third fetches all of the rows, encodes them as JSON and prints the result.

I have timed the execution of all the pages, which ranges from 0.001 to 0.004 seconds. Here are the timings of the Ajax calls however:

Fetch entire table as JSON: 8-12ms

Add row: 990-1010ms

Remove row: 990-1010ms

The response time of the add and remove times are causing unnecessary sluggishness in my application, considering the PHP execution times are mere fractions of seconds, and their output is simply "1" for success and "0" for failure (Compared to the fetch call, which prints out up to 100 rows (~10 columns) of data as JSON)

All my Ajax return timings are measured in Firebug. All Ajax calls are made through jQuery ($.ajax()).

I'm guessing this has something to do with Firefox not considering a GET request answered unless it has received a particular 开发者_Python百科amount of bytes or characters, and the 1 second response time being a timeout of some sort.

Any wisdom to share on the issue?


I've timed the AJAX loading times in Chrome and Opera as well (Averages):

Opera:

* All AJAX calls ~15 ms

Chrome:

* Remove row and fetch rows: ~25ms

* Add row: ~350ms

My application is snappy and responsive in Opera, but sluggish to a varying degree in all other browsers. Exactly why is still unclear to me.


I've timed IE 9 as well, which performs practically identically to Opera. My application now operates swiftly in two browsers.


Apparently some browsers experience mysterious side effects from using localhost rather than 127.0.0.1 in the URL. The added full second of delay was one of them. Using 127.0.0.1 solved the issue for me.


it will be better if you use HTTPWatch.It will tell you the exact time of all js file and css and page rendering etc. in this way you can analyze, which call is taking more time and why.i am not sure that i answered your question or not, but hoping you might get some help from HTTPWatch.

Here you have a good article which will tell you, how to improve site performance by minimizing request time.


I have no information to base this on, but my guess right now is one of these culprits:

  • Slow disk(s) - adding and deleting rows might require more disk access than reads (plus MySQL might be doing query caching)
  • Indexes - MySQL has to update it's indexes when a column is changed or added

Barring that you should profile your code. Use xdebug and/or run your MySQL queries directly via MySQL to see how long they take.

0

精彩评论

暂无评论...
验证码 换一张
取 消