DEV Community

Node.js vs PHP

Sandip Mavani on September 24, 2018

There’s no doubt PHP is the most known and commonly used language for server-side scripting. Before Django and Ruby on Rails gained popularity (200...
Collapse
 
dbelyaeff profile image
Dmitriy Belyaev

These are two separate worlds. I think it's a choice of habit.

I'm programming in PHP from 2003.

Then Rails. Then Node.JS.

All of them are differ. They can be used for the same purposes, but any of them has its own advantages and disadvantages.

No one will deny you to use any stack of technologies in your work.

And if you're not doing large scalable projects performance difference between those languages is not so noticeable.

Collapse
 
buphmin profile image
buphmin

So here are a few thoughts of mine using php/node for both web and non web projects.

For this I am assuming php 7+ and node 8+.

So for web I would give node.js the edge, and for an api only node.js for sure. In my experience node simply has better performance in both req/sec and, imo, more importantly response time. A caveat here is if you are doing heavy cpu processing (especially if you can use a php c extension), because php is typically faster in that regard.

So in my testing (not the most scientific method mind you) on my local linux vm using I was able to handle 40x more concurrent requests on node before degrading response time to over 200ms. I have played around with php-pm with a Symfony project and I was impressed with performance, but it would frequently error for no reason I could figure out.

Now I will say the frameworks for php are better (if you like using a framework). I think this has to do with how long php has been around and that people tend to contribute to fewer but very well maintained frameworks in php. Node has a metric crap ton of frameworks and they all seem to have some issues. PHP will also be easier to maintain, though typescript is making node easier.

Overall though if you are looking to make a website that is not internal use only then the performance difference is really hard to ignore. Especially since js is not all that much harder.

Collapse
 
perttisoomann profile image
Pert Soomann

Not played around with Node that much, but I got the feeling, correct me if I'm wrong, PHP runs every request independently, so breaking code on one URL won't affect other visitors, while on Node project you could crash the whole service, and would need to restart it for the whole site/app to be available again?

Collapse
 
kejtenoviot_sin profile image
Кејтеновиот син ♦

You can make micro services with nodejs so that if one crashes, they don't all crash.

Collapse
 
phunckys profile image
теле • Edited

Both replies to the original comment are workarounds. PHP provides this out of the box, while with Node.js you need to create an architecture around handling such cases.

Collapse
 
vardiak profile image
Nathan

Basic error handling can prevent this from happening. It's native if you are using Express.

  • microservices make that even less possible.
Collapse
 
bgadrian profile image
Adrian B.G.

I had to do a comparison for a new startup actually and (unfortunatelly) I had to choose between these 2 languages, and putting aside the local market available developers, PHP really didn't brought any advantage on the table.

It may behave better with *SQL databases (I found this reason as stated by other devs, but I haven't seen any data on it), but rather then this NodeJS is better at ecosystem, latency, re usage of the code between frontend/backend, and a few others. I will have to search my paper on it.

But between NodeJS and compiled back end languages like Go,Rust and Java enterprise Apps I really don't see PHPs place in any med/large app/team in 2018 and beyond sorry. I think it is still popular because of the sheer amount of developers that know PHP.

Packages

I have not played with composer in 5yrs, but I bet is better than the NPM as a tool and as an ecosystem (which is a total mess).

However, the main advantage of PHP when it comes to working with data is that it’s compatible with all the hosting services.

No real project/customer that has more then 3$ monthly will use a shared hosting, so it is an invalid argument. For more serious hostings managed env nodeJS has more supported environments actually (including Cloud Functions).

handling asynchronous I/O operations isn’t something that can make Node.js a winner in Node.js vs PHP performance competition.

Sure thing it is, is actually why Node will win in intensive web apps. Should I remember you that I/O include any DB and API query?

They require a certain run-time environment to be used for scripting

Even if PHP 7 is a lot better then the previous, let's just say that I trust V8 performance team more than I trust the PHP devs on the long run.

across most popular content management systems

I cannot believe you mentioned CMS and Wordpress in the "Scalability" subtitle.
PHP-FPM is easier to scale then nodeJS that is for sure and well supported. For nodeJS a simple similar alternative would be single-core machines with auto-scaling LB.

Collapse
 
thinsoldier profile image
thinsoldier

No real project/customer that has more then 3$ monthly will use a shared hosting, so it is an invalid argument.

There are hundreds of thousands of freelancers living acceptably comfortable lives by having dozens of clients who are willing to pay a few hundred dollars as-needed but unwilling to pay any kind of monthly fee. And even if they did, not everyone has the time to be an expert in all the areas of expertise they already dabble in and then have to learn to do their own devops on top of that. They'd rather pay monthly for real professionals to manage their server for them IF their clients really needed that, but most of their clients do not need it.

Collapse
 
thinsoldier profile image
thinsoldier

Such an essential operation as an interaction with a database requires less and simpler (quite alike to HTML) code in PHP than in Node.js.

I'm a php fan but I have to disagree here. I've watched tutorials on using MySQL with Node and it seems just as easy as php.

Collapse
 
nezteb profile image
Noah Betzen

I personally prefer PHP 7 to Node. Here is a cool video by the creator of PHP about the language: youtube.com/watch?v=rKXFgWP-2xQ

Collapse
 
lluismf profile image
Lluís Josep Martínez

"For almost 10 years, it had been the only option for a back-end developer." You seem to forget ASP, JSP ... popular template engines for C# and Java.

Collapse
 
biros profile image
Boris Jamot ✊ /

I didn't understand the conclusion: who wins?

Collapse
 
drewknab profile image
Drew Knab

No one wins. There’s just your needs and the needs of your project and choosing a server-side language appropriately.

Collapse
 
nathandaly profile image
Nathan Daly

Just to add to this discussion, PHP 8 will be a true JIT compiled language.

Collapse
 
shalvah profile image
Shalvah

Small correction:

After that, you just need to install http-server packages via npm

You don't. They're included with Node.

Collapse
 
xowap profile image
Rémy 🤖

I think there is a mistake, PHP means "Personal Home Page". It's a weirdly common mistake though, even the PHP docs seems to make it.

Collapse
 
christophwurst profile image
Christoph Wurst

It was just called "personal home page" in very early versions. Ref en.wikipedia.org/wiki/PHP#PHP_3_and_4

Collapse
 
miodragt profile image
Miodragt

plz look at link where I am use react and php
academia.edu/38021343/Pozivanje_ph...

Collapse
 
devmazee2057282 profile image
dewbiez

I've used both PHP and Node.js, and I recommend both of them.

Collapse
 
hesamrad profile image
Hesam Rad

Helpful article, thank you for putting the time to write it.