2

I just came across an answer for javascript and tried out. It actually works in a way that compare to looping through the whole array, it eliminates the screen-frozen effect: https://stackoverflow.com/a/10344560/2083396

So my questions is: is there any similar way in PHP to improve performance?

My understanding is that in the javascript solution, after each timeout callback is called, its scope is destroyed. And thus the resource is released. (did I understand it correctly?)

Is there any way we could do so in PHP and does it help?

Thanks!

Community
  • 1
  • 1
Chen Sturmweizen
  • 639
  • 6
  • 12
  • Now what kind of array do you have that looping through it is making your application lag to the point it's being felt? Why would looping through chunked array improve any performance at all? Where is this notion coming from that it behaves better? You still a) have an array that occupies memory and b) CPU has to go through all its records. What is chunking doing to improve performance exactly if the above mentioned reasons stand true always? – Mjh Mar 02 '17 at 12:06

2 Answers2

2

No, you didn't get it. The only variable that is destroyed there is cnt. It does not improve performance. In fact it make it slower.

The problem they are solving is that long-running js functions freeze the page because js is single-threaded. They split the long job by smaller chunks and run it with small pauses to allow js to process other events.

There is no such problem in PHP, unless you are working with long-running react-based application or something similar.

Alex Blex
  • 34,704
  • 7
  • 48
  • 75
0

My understanding is that Apache will only deliver the page when the full PHP script has finished executing, so reducing the overall execution time, rather than splitting it into chunks, will be the best way to improve performance.

Hitchen1
  • 11
  • 1