Performance wise?

Hosting Advice | 2017 Reviews, Tips, How-To's | Web Experts Let’s begin with simple HTTP requests that can be handled per second. The good guys atran a series of tests [1] . So if you are looking for a server that can handle a lot of concurrent HTTP calls (given that the task to be performed in each HTTP request is a trivial one), basic configured NodeJS outperforms even highly optimized PHP by 17%.

When would you need such a server? One without a lot of CPU intensive work?

Distributing load where HTTP requests are routed to various microservices to perform small tasks. Performing trivial tasks (such as fetching from disk) for a large number of concurrent users

That said, I’d like to clarify one important detail. The performance of a simple “hello world” serving of HTTP request in PHP is heavily tied to Apache and how PHP works. This is how PHP handles a HTTP request:

(Apache/nginx etc) Server receives the request, dispatches it to a PHP process (through forking or by using an idle PHP process) The PHP code is loaded into memory, the PHP script is run. This may involve framework initialisation, configuration setup, connections setup etc (Yes, the same startup script is run for EVERY request) The HTTP request is processed served back to the server. Server delivers the response to the client. The PHP process is then terminated.

Compared with NodeJS :

The HTTP request is received by the server (optional since NodeJS can run on it’s own server) Since NodeJS is a long running task, there’s no startup code that run on every initialisation and the the request is processed immediately. The server (if there is one) then receives the response and delivers it to the client.

Seeing this, it’s easy to see why PHP is actually designed, in a way, to fail the basic “hello world” HTTP request benchmark.

I/O performance

If you want to build an application that is I/O intensive, PHP will stick out as a sore thumb because PHP waits for your I/O. This means that while your PHP script establishes connection with your DB or FS or an HTTP API, that threads is blocked from further computation. This is why although PHP isn’t the bottleneck in I/O bound jobs, it’s very nature makes the whole process slow.

NodeJS on the other hand, will not wait for your I/O bound task to be over. It’ll just dispatch it to the event loop and keep working. Once the I/O bound task is over, the callback for that task takes over.

At Visit [2] , one of our microservices is a Jobs Scheduler that routinely runs various tasks such as backups, engagement activities, logging etc. It involves a lot of HTTP calls per user to various servers (think DB, analytics microservice, engagement microservice etc). If, say, you have 10 HTTP/DB connection requests calls per user and have, say, over 100k users that’s 1 million Async tasks calls! And we’d want to do this every few hours.

The above case on PHP would work horrendously. Because even if each Async tasks call takes 0.1 sec, that’s = 0.1*1*10^6 = 27.7 hours! Also remember, that PHP WILL NOT DO ANYTHING while an async job is being done. It’s blocked. It Stopped. It can’t do anything else.

On Node, even with some kind of throttling and rate limiting, it’ll take less than 5 mins.

Resources

To those who say, that this is only valid for one thread, you guys are not wrong. But again understand I’m comparing one nodeJS script with one PHP script. You can run multiple scripts to in NodeJS and that’ll blow PHP out of the park too. If you say that Apache can open up new threads of PHP for every task so that total time would be much less than 27+hours, so can nginx or apache for NodeJS. Also in that case, PHP would take up much MUCH MUCH more RAM and CPU. Not to mention it will block I/O that’ll be tied up for other PHP threads.

Ease of Writing Code

I think it would boil down to what were you exposed in the past. If you have worked with asynchronous methods NodeJS would feel very different. Experience with some multi-threading would come is handy too! However PHP just seems easier and more natural because of the strictly sequential way that it works. In PHP, Line 3 gets executed after line 2 (even though line 2 has an outbound I/O task) returns. Whereas in Node, that’s not the case. PHP would seem easier to work if you don’t understand async nature of tasks.