Hello guys, in this article will talk about server side programming using NodeJS and PHP. I write this article because there is still many question about PHP is Dying in 2019. Actualy PHP is not dying, but PHP popularity ranking seems always going down. So what happened in PHP and Why I prefer NodeJS than PHP?

History

Before I write the reason why I prefer NodeJS than PHP, I will tell you a little about my history to work as developer. At 2008 I started to learn PHP. PHP is not my first programming language but I use it until this time 2019. Then at early 2014 I started to learn NodeJS, but I do more serious using NodeJS started at 2016 until this time. So I was using PHP for more than 11 years and I using NodeJS for more than 4 years. I know PHP better than NodeJS, but now I prefer using NodeJS than PHP.

PHP Compare with NodeJS

There is many another programming language out there. But why I compare PHP with NodeJS is because these two is so popular and having similarities technology. NodeJS and PHP is an interpreted language. PHP is behind Zend Framework and NodeJS is behind Google V8 JavaScript Engine.

I will not compare about features of NodeJS or PHP, because we already know these two programming language has different concept which PHP is synchronous and NodeJS is asynchronous. So I think this is not fair, this is like comparing between Apples and Oranges, so I will compare about experience in real world with using PHP and NodeJS.

So here is the reason why I prefer NodeJS than PHP for a serious project:

PHP is Expensive

  • Shared Hosting

Many people said that PHP is very cheap, because you can just start your website with small shared hosting. And if you using NodeJS, You are required to buy a VPS hosting. Is this true? Yes this is true in year ago, because today cpanel has support to run NodeJS application and today many hosting provider support NodeJS in shared hosting package.

  • Database in Shared Hosting

Most of PHP application is need a database server. I know that PHP could use json file as database, but just don’t use this way, because PHP is blocking, your database in json file could broken at anytime when you have high request. So you will required a database server like MySQL,MongoDB, etc.

If you think will install or use database server in same shared hosting, actualy this would not make any big difference. Because database server will eat more memory, so your PHP will lack of memory. Most of people will use file cache to decrease request to database, but just don’t do this because your inode is limited.

How about using SQLite? Yes you are able to bypass the inode limit, but actualy, SQLite is blocking, you won’t get much benefit by using SQLite if your concurrent visitor more than 100 visitor. Your PHP will going more slower and maybe will getting request timeout.

  • Conclusion about this expensive

NodeJS is more cheaper. For small application you are not required to use database server, you can just use json file as database and you have no worries about blocking as NodeJS is asynchronous. NodeJS is single thread, multiple request will be queued automatically in background process so you don’t required a proxy server like NGINX. Also you can use memory as cache internally without a cache server like Redis,Memcache,etc.

PHP is expensive because you have to buy another server for database server for the first time. But if you want to install database server in same hosting, you will required more RAM size, so you will still need to increase your payment for your shared hosting.

As my experience, using database server is required minimum 1Gb of RAM, and this still not strong enough to handle more than 100 concurrent queries.

PHP is Slow

The fastest PHP framework is Slim Framework and Lumen, which is could handle 1900 request/second only to echo hello world!.

But the fastest NodeJS framework is fastify, which is could handle 50143 request/second only to echo hellow world!. The slower NodeJS framework is hapi, which is could handle 17870 request/second. Even with the slower NodeJS framework, PHP still can not beat NodeJS speed.

I use framework to compare the speed, because in real world, you will never use native except a framework.

PHP is Hard to Scale

What I mean about scale is the project growing bigger, this is not about scaling the request. PHP will always recompile the script in every request, so PHP will eat more memory if your source code is getting bigger.

Even if I using singleton in PHP, they don’t save the state of variable. PHP can not save the variable persistent in memory except you are using cache in memory like Redis, Memcache, etc.

I have an experience with wordpress, which is my wordpress getting slower as I have many plugins installed in my wordpress. After take a research, I just knew that PHP is always reload all library in every request. So if you have many plugins, it mean PHP will take more memory to recompile it plugin scripts in every single request.

In NodeJS, for a common class when you require a library, this will become singleton, so if you are require for multiple times, NodeJS will not recompile again and again for the same library. So even your project is getting bigger, your application still fast and low in memory consumption. This singleton also support to saving a variable which is persistent.

Final Thought

With all above reason, in my opinion, NodeJS is better than PHP. You can go with PHP for small or medium project and go with NodeJS for enterprise or serious project. I recommend to use NodeJS since PHP is hard to scale. Remember that when you are working in big project, you have responsible to maintain and scale your application.

PHP is simple but NodeJS is not too complex. But it just the code line more longer because mostly to code in NodeJS, you have to playing around with callback and closure.

This doesn’t mean I will stop creating an application with PHP. You have to open mind, that market demand for PHP application is still high. Actualy, I still make a lot money from PHP as freelancer.