Good day. Today, for the sake of sports interest, I decided to check how much faster javascript php is, because I always thought php was faster.

I made 2 identical cycles in both languages, the result surprised me.

Javascript:

var start = new Date(); var x=0; for(var i=0;i<10000000;i++){ x++; } var end = new Date(); console.log('Скорость ' + (end.getTime()-start.getTime()) + ' мс'); 

PHP:

 $begin_time = time() - 1272000000 + floatval(microtime()); $x=0; for($i=0;$i<10000000;$i++){ $x++; } $end_time = time() - 1272000000 + floatval(microtime()) - $begin_time; echo $end_time; 

Everything was done in Denver on the same machine.

Results:

In all browsers, PHP counted the cycle in 1.05 seconds. But javascript turned out to be faster (well, of course, IE let down)

Firefox: JS = 33 ms

Chrome: JS = 149 ms

Opera: JS = 37ms

IE: JS = 606ms)))

Safari: JS = 137ms

In JS firefox, php was 33 times faster. Is this normal? I was just surprised by this situation :)

  • 2
    Somehow not in Russian. time () - 1272000000 + floatval (microtime ()) Better microtime (1) - ReinRaus
  • 7
    The reason for this is not that one is executed on the server, but the second is on the client .. The reason is very simple here - JS Nyasha, and PHP - Kakash: D - Zowie
  • 6
    @AlexWindHope, so be it = / - Oleg Arkhipov
  • Now add to the experiment access to the database or read / write files - korytoff

3 answers 3

JavaScript is executed on the client side, and PHP script is on the server side.

What are you comparing with a young man? With the same success, you can compare the length of a blonde skirt with the colonel's overcoat ...

  • one
    A node js?) - evgeniy
  • four
    Blonde skirt is shorter anyway. Regarding JS: the total capacity of client computers exceeds the capacity of the server , so that it makes sense to reasonably load the client with the work with which he is quite able to cope. Another thing, there are still people who beat themselves with the heel in the chest and assure that it is necessary to support the sloops who have !! Attention !! DISABLED JS. It is disabled, not missing. For such there is <noscript>. - knes
  • one
    Barmaley, server and client = the same my car) I say the same, on Denver everything keeps on - Denis Masster
  • four
    And since when has it become shameful to compare the performance of different PL? Moreover, both can perform similar tasks. T.ch. I believe that the comparison is quite correct, albeit with predictable results. As for the tasks performed on the server and on the client, then you can come up with many scenarios. For example, the most obvious is the generation of HTML from a template. - Ilya Pirogov
  • 3
    @Barmaley, it was necessary to compare the size of the bust, the advantages would be more :) - Ilya Pirogov

In all browsers, PHP counted the cycle in 1.05 seconds.

Do you really think that the speed of processing a request by a web server depends on the browser? :)

But in general, the test is not entirely correct. If you do not take into account that the comparative testing of the speed of the PHP interpreter and Javascript engines causes a "logical malaise" of the brain, you at least execute the script natively - without web servers.

  • you have overdone with the use of such clever phrases and words. I did not understand you nichrome - Denis Masster
  • 2
    Do not bother, it is unlikely to affect the result - activist
  • 3
    @activist - I ran tests “natively”, without a browser shell for JS and without a web server for PHP, the results are almost the same (+ - 5%) - Zowie

server and client = the same my car)

And what from this?

On php you have 30 meters of memory and 30-60 seconds of time, and JS on the client side will get all the possible resources that are on the computer.

And in general somehow it is crazy to compare server and client execution.

What you need to perform on the server, in life you will not give to the execution of the client! IMHO!

  • four
    It's crazy to demand resources, if we are talking about calculating the amount in a cycle. So implementation of PCP is nonsense. - avp
  • It was lazy to comment on the question but, once accepted such an answer ... @Shrek - despite the fact that your statement is delusional by all criteria, let us assume that the reason for this result of PHP is that it depends on Apache, because it lacks memory and etc. Actually - run the interpreter directly and you will see that the reason for such a result is elementary to disgrace and lies in the wretchedness of the PHP implementation. If there are doubts about the browser environment - do not be lazy, put node.js and run a test, the result will be approximately the same ... - Zowie
  • I will enter - run both tests from the console. The only thing you are right about (but this has practically nothing to do with the question itself) is that you don’t give the client what you need to do on the server. Concerning the delusional comparison of client and server - do not forget about node.js. Run the node.js test and the results will be about the same. PS: I liked part of the answer about the operational memory - could you write where you need even those 30 meters? - Zowie
  • In fact, the answer was designed for the last lines. > I was lazy to comment on the question, but once I accepted such an answer ... But I would not believe how lazy I would be to comment on what was accepted :) I will set up a free day and test it. Honestly, until it is gone. - Artem
  • 3
    > The only thing you are right about (but this has practically nothing to do with the question itself) is that you don’t give the client what you need to do on the server. And does this person say Node.js? - Ilya Pirogov