Software and Other Mysteries

On code and productivity with a dash of unicorn dust.

PHP vs. JavaScript

I love the rapid development of the technological wonder that is the Internet and its standards. The early nineties brought us static HTML pages which, but as time moved on more and more processing took place at the servers with a variety of server-side languages. However during the last few years much processing has been transferred to the browser using Javascript. This has meant that the majority of all browsers have gotten along for the ride and spent a lot of time speeding up the execution of these scripts. Out of curiosity I decided to compare the time it takes to perform a few standard actions in both JS and PHP.

To compare the same functionality in the two languages I used the really awesome php.js library, which sole purpose is to replicate as many PHP functions as possible using only Javascript. The tests were then run three times each in two different browsers, Firefox 3.6.6 and Opera 10.54, on my MacBook (Intel Core 2 Duo 2.4 GHz with 4 GB of RAM). The values presented are the average of these runs (for PHP I averaged all the six results).

Test                                       PHP             Firefox 3.6.6       Opera 10.54
Generate 10 000 MD5 hashes                 0.0115 s        3.6770 s            0.7013 s
Generate 10 000 SHA1 hashes                0.0136 s        0.8553 s            0.5703 s
Square root of 100 000 integers            0.0481 s        0.0017 s            0.0033 s
Sum values from 30 000 objects             0.0220 s        0.0010 s            0.0020 s
Sort an array of 30 000 random integers    0.0222 s        0.0343 s            0.0273 s

PHP really kicks ass when it comes to hashing, being about 60 times faster than Opera and a whooping 320 times faster than Firefox on the MD5 hashes. When it comes to calculating the square root it’s not as great though. Opera is about 14 times as fast and Firefox is twice as fast as Opera, so if you’re gonna be crunching numbers you might want to consider letting your users' browser do the heavy lifting.

For the summation test I created an array of 30 000 objects with two attributes, one name and one value, and the same array was used for Javascript by JSON-encoding the PHP variable. Javascript won this round as well, however, to be fair, my guess is that the reason for this is that JS objects have less overhead than their PHP counterparts and not so much it’s mathemagical powers (though that would go well with the square root awesomeness).

Lastly the sorting of an array. Once again the same array was used for both languages, but this time it’s a win for PHP. It’s a close call though, and especially the Javascript powerhouse that is Opera is not far behind.

So this is not the most scientific test around, but I still think the results are pretty interesting. Comments are open, any great ideas of how we can use this for web development or did I just waste five minutes your time?