21

I am writing a script to tokenize file contents. I've written a tokenizer in JavaScript, but it gets pretty slow with large files (5+ seconds). Since I'm retrieving the files from the server anyways, I am considering rewriting it in PHP to shave off a few seconds. I've read that the speed difference between PHP and JavaScript is negligible, but if the files are 1mb or bigger, a small speed difference could add up. I found this benchmark online, but I'm not sure if the info is up-to-date (with all the JS engine optimizations from the past couple years).

Porting over all my code would take some work, so if I can help it, I won't use PHP. But if its going to be much faster (25-30%), it would be worth it.

Azmisov
  • 6,493
  • 7
  • 53
  • 70
  • 3
    One thing to consider is your execution environment. If this is going to happen on the client browsers of end users on a website, you'll have to contend with widely varying execution times, possibly degrading user experience. You're in control if you execute it on the server, but would obviously need to consider concurrency and the associated load. If your JavaScript is to be executed in your own environment (if you're scripting for personal use, for example) that point is moot. – Michael Berkowski Jan 15 '12 at 00:59
  • Generally speaking, most JS engines are much faster than the official Zend PHP engine. The cliché example being that you would be hard pressed to write a serious ray tracer in PHP, but very feasible in JS (there are many out there). – coreyward Jan 15 '12 at 01:00
  • tokenising something server side (whatever language) give the possibility to _cache_ the result benefiting multiple users (or even the same user when they refresh the page) - as well as being independent of the client's hardware/activities. – AD7six Jul 19 '13 at 15:21

4 Answers4

18

Well, like you alluded, JavaScript is executed on the client side, so it would depend on the client. For example, a client on an iPAD wouldn't be able to run those Quake3 HTML5s as smoothly as a client running on a gaming machine. PHP on the other hand runs on the server.

In general, JS is faster considerably than PHP though (on the same hardware);

Here are some [hard figures][1] of tests between the two, to back up my assertion. [JS (query) vs PHP][2] Performance.

[1]: https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/php.html [2]: jQuery vs. PHP - Performance Comparison

Authman Apatira
  • 3,994
  • 1
  • 26
  • 33
  • 28
    JS runs on the server as well. –  Jan 15 '12 at 01:03
  • 1
    Perhaps I'm looking at it wrong, but [the box plot](http://shootout.alioth.debian.org/u32/which-programming-languages-are-fastest.php?php=on&v8=on&calc=chart) makes it look like V8 JavaScript is faster than PHP. – icktoofay Jan 15 '12 at 01:05
  • OOPS, that should have been reversed. Thanks @ ick2 – Authman Apatira Jan 15 '12 at 01:07
  • Thanks for the link; quite helpful. – Azmisov Jan 15 '12 at 01:13
  • JS faster? :DDD nice joke. – Tommix Jun 03 '15 at 12:54
  • It is faster @Tommix. Perhaps you're confusing browser-side JS on some old machine. Anyhow, you don't have to take my word for it, just google it. NodeJS / IOJS / V8 / etc so many JS tech stacks out there and they all Kill php's performance. Even companies that still code in PHP (Facebook) had to write HipHop_for_PHP in order to compile their slow PHP code to C++ since it's performance is so terrible... – Authman Apatira Jun 25 '15 at 19:47
  • 1
    Great links supplied - updated to http://benchmarksgame.alioth.debian.org/ as it is now 64bit not 32. – Ukuser32 Feb 26 '16 at 14:56
  • 1
    The link is dead, get the new one: https://benchmarksgame-team.pages.debian.net/benchmarksgame/fastest/php.html – Acuna Aug 05 '20 at 17:51
  • I can't believe this answer is still up here – Bjorn Lottering Mar 02 '23 at 12:56
4

Its worth saying that another consideration for using PHP is the ability to cache server side. I'm looking at this considering if I should move some of the functions from JS to PHP because if I then cache the files server side there is no further calculations involved at all! I know this ticket is old but this may be another consideration for future readers.

Ukuser32
  • 2,147
  • 2
  • 22
  • 32
3

I just converted a script from PHP to Javascript and I have greatly increased performance.

Javascript seems much faster in mathematical calculations. I have personally witnessed the performance improvement, my PHP procedure in some cases also employed a few seconds of running, while the same procedure written in javascript takes at most a few milliseconds.

Although Javascript to be known as client-side language I run my code on the server side thanks to Node.js

Fifi
  • 3,360
  • 2
  • 27
  • 53
Zauker
  • 2,344
  • 3
  • 27
  • 36
-1

You cannot expect php to be able to do long Mathematical calculations like your tests show, it was never built to do that like Js was. You are judging a monkey and elephant on how fast they can climb the same tree.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/34454774) – Ferris May 28 '23 at 10:43