DEV Community

Discussion on: PHP or Node.js: The right technology for your project

Collapse
 
joelbonetr profile image
JoelBonetR 🥇 • Edited

First of all the comparison is between JavaScript and PHP. Node is just a runtime environment so it cannot be compared to a language (it's said in the post but I need to clarify this many times). We'll understand "Node.js" as JavaScript running on a server over Node from now on the following lines.


Similarities:

Both are scripting languages, both are interpreted (but also both are compiled by the server to gain efficiency), both lack of strong typing, both are single-threaded (you can currently use more than a thread on both with different approaches).


Use case

The main question is that if you need async or not. If you are going to code a whatsapp knock-off you'll definitely need Node. Handling this amount of requests synchronously would be a mess.
On the other hand if you are going to develop a blog, both tech can be equals fine.

TL;DR: if you need Async, use NodeJS, if you don't need Async, both are great unless you need high CPU demanding tasks, in whic case none of those two languages would be the best. You better use (Python, Kotlin, Java, C#, C++, Go, Rust...) for this


Performance

Even the given similarities, these technologies adhere to different concepts in handling requests: PHP has a slower, synchronous code execution procedure, which means that every module or function is processed in the code-specified order. If one query is not executed, the next one will not get executed until the previous one is accomplished.

The process of opening a file in PHP looks like this:

  1. PHP sends a query to the computer's file system.
  2. Waits until the file system opens and reads the requested file.
  3. Returns the content to the client.
  4. Works on the next request/code line.

Owing to the powers of the V8 engine, real-time server interaction, and asynchronous execution (order-independent execution), Node.js can boast superior performance and obviously outperforms PHP.

This is how Node.js concurrently handles a file request:

  1. Node.js sends a query to the computer's file system.
  2. Works on the next request without waiting for the previous one.
  3. When the file system has opened and read the requested file, the server sends the content back to the client.

Conclusion:

Because of a slower loading process rendered by PHP, Node.js is the winner in terms of performance and code execution speed. It eliminates the waiting downtime and provides real-time data in a much more efficient manner, which allows using Node.js for high i/o load projects.