PHP vs Node.js
According to the “Developer Survey Results 2016”, which was conducted by STACKOVERFLOW, among over fifty thousand developers from all over the world, PHP is more widely used back-end technology compared to Node.js among back-end developers. Source: stackoverflow.com/research/developer-survey-2016
- Terminology differences between PHP and Node.js
- Behind Node.js Creation
- PHP 7 vs Node.js Performance
- PHP has learned its lessons from Node.js to be even stronger
- PHP 7 Vs Node.Js: some opinions from the web
PHP 7 VS NODE.JS: TERMINOLOGY DIFFERENCES
- PHP cannot work without a web server. Node.js has built-in tools for creating servers, hence it can work without the help of external servers. However, as a rule, Nginx server is used as a reverse proxy before Node.js server on a production hardware server.
PHP 7 VS NODE.JS: BEHIND NODE.JS CREATION
- The limited possibilities of the most popular web server in 2009, Apache HTTP Server to handle a lot of concurrent connections (up to 10,000 or more);
- The most common way of creating code (sequential programming), when code either blocked the entire process or implied multiple execution stacks (read as “more hardware and software”, more money) in the case of simultaneous connections.
- Node.js provides a purely evented, non-blocking infrastructure to script highly concurrent programs.
Ryan Dahl‘s presentation was regaled with ovations. One of the most common web applications' performance problems was resolved! No additional hardware! No additional monthly hosting costs! The only strict requirement was retraining programmers to teach them using event loops, callbacks, and non-blocking I/O.
The disadvantages of being single-threaded: your code will run in single thread only and so you can’t do anything complicated. If your code needs to process data for each request and it’s going to take just 10 milliseconds and there are 1000 concurrent connections, it will take 10 seconds to finish all of them. It’s pretty bad response time, isn’t it?
Some large companies expressed interest in the implementation of Node.js into their live project. As was reported, the transition from Rails to Node.js improved the scalability of LinkedIn’s mobile backend infrastructure. There’s been a lot of talk about PayPal moving from Java to Node.js for an application platform.
PHP 7 VS NODE.JS PERFORMANCE
PHP 7 is based on the PHPNG engine that speeds up PHP applications more than the previous PHP interpreter (Zend Engine 2.0). Thanks to PHPNG, your apps see up to 2x faster performance and 50% better memory consumption than PHP 5.6
With PHP 7, possible differences in performance are already not an issue when one compares Node.js with PHP.
But even earlier, in 2013, Phil Sturgeon made a test and concluded: "PHP is not sucking as bad as some people would expect. Removing the blocking PHP approach (because obviously, it's slow) and running just the other three scripts [PHP 5.5.5 + ReactPHP + phpQuery] looks like this [take a look on the image below]. Look, they're the same. At this point, it is just a network test. The speed between the two systems for handling this specific task is essentially identical".
Need Node.js or PHP developers? Hire a dedicated team!
PHP 7 VS NODE.JS: PHP IS ALIVE AND HAS LEARNT ITS LESSONS FROM NODE.JS TO BE STRONGER THAN BEFORE
A lot has changed since 2009. The WebSocket protocol was standardized in 2011 and NGINX server started to support it. PHP programmers have learned to use event loops, callbacks, and non-blocking I/O with ReactPHP.
So, today’s experienced developers can choose what tool to use for any particular project. What do they take into account when considering what tool to use?
Andrey M., a software developer at Belitsoft Company (10 years of experience with PHP, 1 year – with Node.js): For some tasks, Node.js may be faster than the “standard” webserver with PHP because it runs as a single thread with non-blocking IO for each connection, hence there is no memory overrun. Due to this, Node.js is useful when there is a need to process data in real-time (chats, games, video, big data streams without logic). However, Node.js can also be used for creating CMSs, e-commerce, and other web applications, which are usually developed using PHP. Node.js is also able to perform high-load tasks, but these tasks should be moved away from the main thread using appropriate tools such as callbacks, workers etc.
On the other hand, tasks for which Node.js is an excellent choice can be performed using PHP. For example, ReactPHP enables developers to write PHP-based socket server to process requests constantly as well as Node.js does it (yes, Node.js is faster, but the fact is that PHP can also do it). The same thing with Workers (the classes responsible for running asynchronous jobs and synchronizing their results) or Amp (libraries that are used for writing non-blocking asynchronous code). Hence, it is easy to create long running processes using PHP. Additionally, there are a lot of tools for supporting and managing these processes (such as supervisord).
Finally, Node.js can be used as complement to the PHP (or to another programming language) with the aim of reducing load on a server and performing program (or, as one can say, PHP can be used as complement to Node.js :))) For example, the first version of a website was built in PHP and then the client asked to develop a real-time system that gets data from stock data systems. I would prefer to develop that real-time system as a separate application using Node.js (and then integrate it into the PHP-based application).
So, the same tasks may be performed either with PHP or with Node.js. The question "what tool to use" is a question of personal preferences. I use Node.js for tasks involving big data flows and PHP for tasks involving complex logic, high-load tasks, for dealing with external utilities, applications, and OS. PHP has a lot of different libraries, frameworks, and it is more standardized than Node.js (however, Node.js is not far behind and continues to gain momentum). In general, I think it is a good idea not to counterpose them, but use them cooperatively.
Sergey K., a software developer at Belitsoft Company, participant and award winner of various hackathons (8 years of experience with PHP, 1 year – with Node.js): Comparatively low entry barrier for writing PHP code means there are many PHP specialists on the market, hence, it is pretty easy to find developers for PHP-based projects. Due to age factor, PHP has more production-ready modules, frameworks, and CMSs. It means if a client needs a product and there are ready solutions in PHP, then it is better to use PHP because it will be cost effective.
PHP and Node.js are tools that can very often be used interchangeably for the same tasks. However, Node.js is often used for real-time applications, web sockets and workers (especially, persistent workers), and rarely for high-load tasks and computation. From the scalability perspective, there are no big differences between PHP and Node.js, it is more important to consider the project’s architecture.
It is always interesting to hear what other experienced developers think. Here is what we have gathered from the web.
PHP 7 VS NODE.JS: SOME OPINIONS FROM THE WEB
Jonathan: We are planning to run a large-scale E-commerce marketplace (eventually a pool of websites, one for each country) and we are trying to decide on whether to use PHP + Apache Stack or go with Node.js. We plan to get 5000 peak simultaneous connections for our "product search" function. What would you suggest?
Thomas Hunter (a Senior Node.js Engineer): I'd lean towards PHP as you're probably not going to benefit from any real-time communication, shared data between different users, etc. Also, if Node crashes, it could cause a lot of trouble, especially since most of what you are doing is transactional, but if one PHP execution fails, it's much less dangerous. There's also a lot of PHP eCommerce applications and libraries out there already that you could make use of.
Juan (a developer with experience on Node.js, PHP, and Ruby): I developed an amazing and faster webRTC solution using Node.js and an astounding video portal using PHP that receives 1, 5 million visits every day; however I don’t think that I can achieve these projects’ goals by interchanging those technologies.
Laurens: I still doubt whether to use Node.js or native PHP sockets. Wouldn’t it make more sense to use default PHP instead of Node.js? As far as I know, Node.js is limited and barely has support for a good MYSQL connection, isn’t PHP more optimized for this? I know that Apache is slower than Node.js, but for what kind of application is it better to use Node.js? I do not have any idea of Node.js advantages over PHP Socket, perhaps the speed, but I will be using Nginx.
Gonzalo Ayuso (an experienced web architect and web developer): WebSockets with Rachet (PHP) works fine. I started to play with socket.io (node.js) some time ago and now I’m very comfortable with it. I haven’t done any in-depth benchmark to choose one or another, indeed. Socket.io for example, has a very fail-over system to work with old browsers (it works even with IE6), but Rachet works fine with newer ones. As you said, to perform traditional SQL operations is a bit of a nightmare using node. This is mainly because of the functional nature of js (OK js is not a fully functional programming, but it’s very close). I don’t have this problem because I always rely on backend server (PHP for me) and I only use node.js for the strictly real-time operations. My node.js servers for WebSockets are just a proxy and all the logic is bypass to the backend. Anyway, if you choose PHP or node.js for WebSockets you must take into account that you will need to handle two different servers. That’s means share authorization layer and things like that.
Since WordPress.com has moved to Node.JS, do you think Laravel will also look towards that option even though PHP 7 is here?
Dayle Rees (a Laravel Framework contributor and developer): For a long time PHP was the butt of many language jokes, but I honestly feel that it’s becoming not only a popular language but a powerful one. PHP7 is great. The speed boost is one thing, but having optional support for full type hinting is a game changer. We’ve also got modern tools like Laravel and Composer, breathing new life into the language and its supporting community. With this in mind, I think it’s unlikely that Laravel will move from PHP. I think it’s more likely to gain further integration with front-end tools to provide a complete application building platform. That’s where I see it heading in terms of future expansion. I’m sure Node will continue to excel when dealing with microservices and threaded applications.
Contact us and get a free quote on your PHP or Node.js project!