April 3, 2012

Can AJAX or PHP "echo" too much?

Question by Marc Brown

This a question that is more on the lines of server efficiency and availability.

Say I have a php page that calls a AJAX script and 1 million users are connecting within the same second.

Would there be a performance boost if I limit the AJAX script so that it only echos once instead of every time it receives data?

I planned on printing all the data to a variable and then echoing that variable once the script is finished.

I’m not sure if echoing is simply storing the data in the server until the script finishes, similar to what I want to do above or if it is actually connecting with the client each time?

If a connection is made for each echo then that would be better than filling a variable with data, possibly causing the RAM to fill up fast?

This AJAX script is pulling data from a database (calls PHP page). I have a lot of “echo” statements that are simply printing table, div, tr, etc tags and then finally the data from the database. Then, once again printing table, div, tr, etc tags. Your saying that it’s better to simply fill a variable with this data and print/echo ONCE?

Thanks,

Answer by Starx

Generally, servers processes scripts and sends the output in the form of HTML, so there is no longer link between a page and server. When you are making a AJAX request, you will open the connection again and send the request.

The main bottleneck in the performance occurs from the amount of request you send to the server. You should limit the request as much as possible.

If a connection is made for each echo then that would be better than filling a variable with data, possibly causing the RAM to fill up fast.

You are wrong about this, You should minimize the request as much as possible, especially, when you are processing a large number of people.


Next, Its nearly pointless to update the content onevery pass. Look how facebook updates its content. The timers and contents are updated every minute. Except the notification part.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!