TOP-R-ad

Random Posts

random

PHP vs Node.js - A fair comparison

What is Node.js?

Node.js is an efficient and scalable software platform that can execute JavaScript code on a server. It uses event-loops rather than traditional threading, which makes it faster and less memory-intensive than traditional platforms. Node uses Google’s V8 virtual machine to execute JavaScript, and can be implemented on Mac, Windows, and Linux operating systems. The only alternative is Java’s Rhino (now called Nashorn), which can execute strings of JavaScript code.

Node.Js vs PHP

In Defense of PHP​

Node.js is not "faster" than PHP - only the efficiency of implementations using either technology can be compared, not the technologies themselves. Some things will be faster in PHP, and others will be faster in Node.js.
For now, let’s talk about coding. 

PHP Apps are Quicker to Make: A Testable Hypothesis

PHP is the simplest language to write small-to-medium sized web apps that require one’s own server. Any counter-argument to this is probably an exceptional case.

Here is a case in point. Last weekend, along with a friend, I entered a hackathon, deciding to create a social payment system using Node.js. I had created and sold a similar system in PHP three years ago, which took me 48 hours to build. Since I had done this before in PHP, I thought it would be trivial to recreate in Node.js. 42 hours later, I just barely had a prototype to pitch to the audience and investors. Not because my team was bad at Node, but because it just takes a little longer to implement each feature. We came in second place, while a more swiftly written PHP version might have taken first. Node.js was the wrong choice for the hackathon.

One of the reasons that PHP apps are quick to make, though, is that they allow for a lot of bad practices. So, you can write a somewhat crappy program in a shorter span of time. Move quickly and break things. Sometimes this is okay.

The "designer" of PHP probably says it best himself:
Rasmus: "In the end, what I think set PHP apart in the early days, and still does today, is that it always tries to find the shortest path to solving the Web problem. It does not try to be a general-purpose scripting language and anybody who’s looking to solve a Web problem will usually find a very direct solution through PHP. Many of the alternatives that claim to solve the Web problem are just too complex. When you need something up and working by Friday so you don’t have to spend all weekend leafing through 800-page manuals, PHP starts to look pretty good."

The 3 Reasons to Use Node.js

1. Node.js implementations will generally run faster

There is a problem with saying that Node.js is faster than PHP. It’s not. There is a trivial and humorous proof: No programming language in the world is, or will ever be, faster than the PHP “Hello World” app:

Hello world in PHP:
Hello World

Hello world in JavaScript (without creating the server):
console.log('Hello World');

However, this is sort of a cheat, and for most common implementations and third-party packages, it is true that Node.js will out perform PHP. In one test between Ghost and Wordpress, for example, Ghost on NginX was over 6 times faster for certain operations than Wordpress on Apache. The reason for this is that JavaScript is non-blocking, while PHP waits for each task to complete before it begins its next task (which also requires more memory). Similarly, for “web scraping,” a common Node.js library outperformed a common PHP library by about 6 times. In this case, the file_get_content() PHP function slowed it down considerably. What if the test were to use non-blocking PHP code instead? Luckily, someone has tested this, and found that the PHP code was faster than Node.js.

In fact, even when NodeJS used a higher-than-default maximum number of connections (64 rather than 5), its performance was practically the same as non-blocking PHP at web scraping.
Node.Js Comparison
From http://philsturgeon.co.uk/blog/2013/11/benchmarking-codswallop-nodejs-v-php
The take-away point is that many common PHP implementations and third-party packages are less performant than their Node.js counterparts will be. This is largely dependent on the quality of the third party developer, and not the language or platform, which is why community is a huge part of this decision. For example, if you are implementing a blog on your site then the chances are you will use a third-party package such as Wordpress or Ghost. In that case, the Node.js implementation, designed to Node’s strengths, will probably be a significant constant (say, 7) times faster. This argument can be run for countless packages that your app might depend on, and therefore Node.js software will very often come out performing faster. But this not because the “language” is faster. The speed of JavaScript on V8 vs. PHP has been argued with application-specific benchmarks elsewhere.

2. Node.js is Well Suited to Integrating Technologies

In 2009, Om Malik wrote about the web, saying that:
“The web is transitioning from mere interactivity to a more dynamic, real-time web where read-write functions are heading towards balanced synchronicity. The real-time web, as I have argued in the past, is the next logical step in the Internet’s evolution... More and more people are publishing more and more “social objects” and sharing them online. That data deluge is creating a new kind of search opportunity.”
To add to that, most new good technologies have interactive features which rely on multiple APIs. Together, this means that the server is a “middle-man,” doing a lot of switching between user requests for both simple and complex objects, and communication with other servers. Moreover, there is usually no reason for the end user to have to wait while these operations are performed.

Alex McCaw gives a good example about non-blocking frontend interfaces. When a user sends an email on Gmail, there is no reason to show a “sending…” alert at the top of the page. For all intents and purposes, once I’ve clicked “send” on my email, the email is sent! No-blocking servers and user-interfaces take this approach, though it takes a while to get used to creating projects in this way.
Because Node.js is event-oriented, it is naturally well built for this.

3. Good Coders are Excited to Use Node.js

Hackers want to hack on Node.js, which is important when you are trying to hire good programmers. I can hardly stress this point enough. You want your coders to be excited about your technology stack.

There is a paradoxical fact about hiring good coders. Good coders want to use the latest technology, even if it’s only marginally better, and this keeps them out of the mainstream. Convincing a good hacker to use PHP is a challenge; getting him or her excited about a PHP project is nearly impossible. Meanwhile, finding smart and creative coders who fancy harder-to-use language such as Scala or Scheme is actually quite easy. For now, Node.js is approaching a good midpoint between being new and exciting, and having been tried and tested.

In Paul Graham’s essay on the future of Java, he had the following four argument about why the language will fail:
“No one loves it…I’ve never heard anyone say that they loved Java…People are forced to use it. A lot of the people I know using Java are using it because they feel they have to.”
“It's bureaucratic...Really good language aren’t like that. They let you do what you want and get out of the way.”
“The wrong people like it. The programmers I admire most are not, on the whole, captivated by Java. Who does like Java? Suits, who don't know one language from another…”
“Like the creators of sitcoms or junk food or package tours, Java's designers were consciously designing a product for people not as smart as them.”-- Paul Graham in Java's Cover
I still think Graham was right on the mark, despite Java's obviously massive success in enterprise software; Java just never seemed to take off with pioneering coders. Few teams ever got excited specifically about using Java for their product.
Now think about Node.js. No business is forcing its employees to code in Node.js, and yet there are coders around the world writing open source books on it. In their free time, hackers at big companies such as Paypal are writing open source Node.js frameworks, to patch up its weaker points. A world of smart people are gathering around the Node.js movement, and now is the time to invest in them.

So, ought I to use Node.js to build my next application?

People will often say that it depends upon the type of application you are building. But nobody really knows exactly what application they are building before they build it. Software development meanders, and companies that have versatile infrastructure are at an advantage. Zuck didn’t know what Facebook would become when he started hacking it in Kirkland. Had he known, he might have resisted using PHP, which Facebook has had to take massive strides to optimize.
The success of your project will not depend much on the language itself in the end, so don’t trash languages. The specific algorithms and the people who are writing that code will be more important. Node.js had an advantage in both of those factors.

Reposted From : graemeboy.com
PHP vs Node.js - A fair comparison Reviewed by Unknown on 02:24:00 Rating: 5

5 comments:

All Rights Reserved by Node.Js Magazine © 2015 - 2016

Contact Form

Name

Email *

Message *

Powered by Blogger.