Leave your details and our sales team will get in touch shortly

Will PHP Survive the Attack of Node.js

Yamini PHP

PHP has completed 15 years in the market this year. However, now other technologies are trying to take over its position. The newbies like Node.js is giving a head on competition to PHP. Let us discuss that what makes the new technologies take over the old ones. Now, let us proceed with knowing the reasons which made PHP gather so much attention since last 15 years. Following are the points which made PHP the most widely accepted server technology:

  •  PHP can be directly used with the designing language such as HTML by mixing its template files with the code.
  • PHP did not require a source code compilation like C++ and java. Thus, it was an interpreted language in itself.
  • PHP was different form the complex object oriented programming languages and was functional in nature.
  • The hosting provider for Apache servers with a Linux, Apache, MySQL and PHP (LAMP) stack were quite reasonable.

Read More:- PHP web services

Later on there came certain irregularities which lowered the craze of PHP. It was vulnerable to security threats by practices of SQL injections, subpar performance, no centralised packaging registry and inconsistent API.Moreover, nowadays there are frameworks such as Ruby on Rails, Django, Node.js and many more substitutes of PHP are available. However, in my opinion Node.js is the best one.

Talking about its functionality, it is similar to the PHP, Apache or ASP and IIS stacks. The platform uses JavaScript and its feature of non-blocking I/O technique increase it performance. Furthermore, it has a Node Package Manager solution and a specification, i.e., ECMAScript. Due to these features Node.js is being widely used by the developers.

node2js

 

Moreover, another important feature of Node.js is that it is asynchronous and event driven. It has utilised the concept of idle code that unnecessarily hogs the memory and only waits for I/O. For instance, an operation that waits for a file to be copied to a disk or for a database server query to return data. Thus, in Node.js every single function is non-blocking.

The feature which makes Node.js different form its contemporaries is that in this you have the authority to direct it node to execute statements. As whenever you ask node to perform a task, you do not wait for the results. Here, you direct it to execute the desired work and then it can carry on with the regular work.

This in return leads to completely different permutations of code structuring which comprises of callbacks and unknown functions and closures. The code is mentioned below:

[code language=”javascript”] doSomething(val, function(err,result){
doSomethingElse(result,function(err,res){
doAbra();
doKadabra(err, res, function() {


});
});
});
[/code]

Although, it is easy to end up with intense nesting, but in my opinion it hampers the legibility of the code. Furthermore, another feature which is distinguishes it from PHP is that there is no need of using separate http(s) server. However, you can keep Node.js behind a Nginx (a web server). Therefore, it only requires an actual web server implementation.

Fair Judgement:

It is not fair to compare PHP and Node.js as both differ drastically. So in order to make the competition fair you need to compare PHP+ Apache2 (any http server) and Node.js. However, you can also check with other http servers such as Nginx or Lighthttpd for PHP. However, the major performance of PHP depends on multiple processes running on the server side. So it’s no point in thinking about the http server as the performance is exclusive of the server. Thus, we can use the standard combination and proceed.

Beginning the test:

In order to test both the web development platforms we will write a code in both of them and then compile them to see the results. So, in this application we will fetch information from 100 rows from a WordPress application and the output will be a json string. Here we are making it simple because, I guess we are testing the two platforms, not me and moreover we just need to compare their performances.

Node.js (javascript):
[code language=”javascript”], // Including the http module
var http = require(‘http’),
mysql = require(“mysql”);
// Establishing the connection.
// Data is default to new mysql installation and should be changed according to your configuration.

var connection = mysql.createConnection({
user: “wp”,
password: “****”,
database: “employeetable”
});
// Create the http server.
http.createServer(function (request, response) {
// Attach listener on end event.
request.on(‘end’, function () {
// Query the database.
connection.query(‘SELECT * FROM wp_options limit 100;’, function (error, rows, fields) {
response.writeHead(200, {
‘Content-Type’: ‘text/html’
});
// Send data as JSON string.
// Rows variable holds the result of the query.

response.end(JSON.stringify(rows));
});
});
// Listen on the 8080 port.
}).listen(8080);
[/code] PHP code:
[code language=”css”] <!–?php $db = new PDO(‘mysql:host=localhost;dbname=employeetable’, ‘wp’, ‘names’); $all= $db—>query(‘SELECT * FROM wp_options limit 100;’)->fetchAll();
echo json_encode($all);
[/code] Results:
In order to test them, I used Load Impact test with very simple configurations. The test parameters are as followed:

  • The number of users is between 0-500 and the time stamp was 5 minutes.
  • Single source of traffic.
  • Enabled the server metrics.

Let’s see the report card

prefork model

Here, we have tested the platforms on the basis of their response time with the number of active users. We noted the response time of each of the platform and kept on increasing the number of users. Let us see the results.

The response time remains constant throughout the test in the case of Node.js server. Moreover, if we talk about the test results of PHP they were very much different from that of Node.js. Initially the response time was quite stable but as soon as the users increased the response time began to drop drastically.

The Difference Lies in

After deep analysis, I found out that the difference in the performance is due to the difference between their model architecture. So let us figure out the difference.

In PHP there is concept of child process. So when Apache works on the PHP page it assigns the PHP execution to a child process. However, the child process is able to execute only one request at a particular instance of time. Wherefore, the other requests have to wait for a child process to get free. So in order to build a high performance system you need to strike a balance between the numbers of requests and server resources.

However in Node, there is only one single process on the server. And this one process is there with one active request handling thread. Therefore, there is no internal process communication between the platforms. Moreover, Node is 30% faster than PHP.

Nothing is Perfect:

After all the good talks, let us talk about the flaws of Node.js. As we have discussed earlier that in Node there is only a single process with one thread. Thus, it is quite complicated to work with multi core server.

Talking about the commercial aspect, PHP has grappled the complete market. Moreover, it is difficult to find proficient coders, tutorials, study material and books on Node. As it is new entry in the technical world and in contrast PHP has a foothold on the market since past 15 years.

Read More :- PHP developers services

Summary:

The two server technologies discussed above vary from each other. One is ruling the market and another one is just an entrant. As we know that there is a lot of competition in today’s world. So in order to be the winner you need the best development platform in order to develop a high performance web application. Moreover, your own preferences will affect your choice. At the end we need to accomplish the sole aim of building a web app which can endure high traffic load and is scalable. We will be glad to make the picture clearer. Feel free to drop us a line at [email protected] .

 

Last modified: 22 Jul 2015