What are the benefits of using Node.js?

Let’s compare NodeJS with one of the most popular and widely tested PHP since I’ve been working on them for the last couple of years!
Performance wise?
Let’s begin with simple HTTP requests that can be handled per second. The good guys at Hosting Advice | 2017 Reviews, Tips, How-To's | Web Experts ran a series of tests[1]. So if you are looking for a server that can handle a lot of concurrent HTTP calls (given that the task to be performed in each HTTP request is a trivial one), basic configured NodeJS outperforms even highly optimized PHP by 17%.
When would you need such a server? One without a lot of CPU intensive work?
  1. Distributing load where HTTP requests are routed to various microservices to perform small tasks.
  2. Performing trivial tasks (such as fetching from disk) for a large number of concurrent users
That said, I’d like to clarify one important detail. The performance of a simple “hello world” serving of HTTP request in PHP is heavily tied to Apache and how PHP works. This is how PHP handles a HTTP request:
  1. (Apache/nginx etc) Server receives the request, dispatches it to a PHP process (through forking or by using an idle PHP process)
  2. The PHP code is loaded into memory, the PHP script is run. This may involve framework initialisation, configuration setup, connections setup etc (Yes, the same startup script is run for EVERY request)
  3. The HTTP request is processed served back to the server.
  4. Server delivers the response to the client.
  5. The PHP process is then terminated.
Compared with NodeJS :
  1. The HTTP request is received by the server (optional since NodeJS can run on it’s own server)
  2. Since NodeJS is a long running task, there’s no startup code that run on every initialisation and the the request is processed immediately.
  3. The server (if there is one) then receives the response and delivers it to the client.
Seeing this, it’s easy to see why PHP is actually designed, in a way, to fail the basic “hello world” HTTP request benchmark.
I/O performance
If you want to build an application that is I/O intensive, PHP will stick out as a sore thumb because PHP waits for your I/O. This means that while your PHP script establishes connection with your DB or FS or an HTTP API, that threads is blocked from further computation. This is why although PHP isn’t the bottleneck in I/O bound jobs, it’s very nature makes the whole process slow.
NodeJS on the other hand, will not wait for your I/O bound task to be over. It’ll just dispatch it to the event loop and keep working. Once the I/O bound task is over, the callback for that task takes over.
At Visit[2], one of our microservices is a Jobs Scheduler that routinely runs various tasks such as backups, engagement activities, logging etc. It involves a lot of HTTP calls per user to various servers (think DB, analytics microservice, engagement microservice etc). If, say, you have 10 HTTP/DB connection requests calls per user and have, say, over 100k users that’s 1 million Async tasks calls! And we’d want to do this every few hours.
The above case on PHP would work horrendously. Because even if each Async tasks call takes 0.1 sec, that’s = 0.1*1*10^6 = 27.7 hours! Also remember, that PHP WILL NOT DO ANYTHING while an async job is being done. It’s blocked. It Stopped. It can’t do anything else.
On Node, even with some kind of throttling and rate limiting, it’ll take less than 5 mins.
Resources
To those who say, that this is only valid for one thread, you guys are not wrong. But again understand I’m comparing one nodeJS script with one PHP script. You can run multiple scripts to in NodeJS and that’ll blow PHP out of the park too. If you say that Apache can open up new threads of PHP for every task so that total time would be much less than 27+hours, so can nginx or apache for NodeJS. Also in that case, PHP would take up much MUCH MUCH more RAM and CPU. Not to mention it will block I/O that’ll be tied up for other PHP threads.
Ease of Writing Code
I think it would boil down to what were you exposed in the past. If you have worked with asynchronous methods NodeJS would feel very different. Experience with some multi-threading would come is handy too! However PHP just seems easier and more natural because of the strictly sequential way that it works. In PHP, Line 3 gets executed after line 2 (even though line 2 has an outbound I/O task) returns. Whereas in Node, that’s not the case. PHP would seem easier to work if you don’t understand async nature of tasks.

Post a Comment

37 Comments

  1. IT's very informative blog and useful article thank you for sharing with us.
    Node JS Training
    Node JS Online Training

    ReplyDelete
  2. It's a wonderful post and very helpful, thanks for all this information.
    Full Stack Online Training

    ReplyDelete
  3. Thank you for sharing this useful blog on Node.js. You have provided very important information on this blog. Eternal Web provides Mobile or Web Applications as well as Module Development Services. Our Developers will deliver you outstanding Node.js Development Services. Kindly contact us on.

    ReplyDelete
  4. Enjoyed reading the article above, really explains everything in detail, the article is very interesting and effective. Thank you and good luck for the upcoming articles learn Node JS training

    ReplyDelete
  5. Thanks for the post , This is such an informative post . If you are looking for the Nodejs development company . So here is the list of Top 10 Node.js Development Companies . Thanks for the post.

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. This comment has been removed by a blog administrator.

    ReplyDelete
  8. ExpressTech Software Solutions is a nodejs Development Company, where we provide the leading Node JS web development services to our clients across the globe. Contact us: +91-9806724185 or Contact@expresstechsoftwares.com

    ReplyDelete
  9. Best article, very useful and explanation. Your post is extremely incredible. Thank you very much for the new information.
    Node Js course with placement in Hyderabad
    Node Js course in Hyderabad with placement
    Node Js Training Center in Hyderabad

    ReplyDelete
  10. Excellent Information and nice work that has been providing here. Keep on update more things.

    Mean stack online training
    Mean stack training in hyderabad

    ReplyDelete
  11. Nice and good article. It is very useful for me to learn and understand easily.
    Full Stack Training in Delhi
    Full Stack Training institute in Delhi

    ReplyDelete
  12. There is a high demand for this advanced JS framework among web developers. It lets them develop robust websites and applications for different verticals. And these are the reasons which push business to hire node js web development companies out of leading node js development companies available in the global market for your website requirements.

    ReplyDelete
  13. Thank you for sharing this informative post. Looking forward to read more.
    Professional Website Development Services

    ReplyDelete
  14. This comment has been removed by the author.

    ReplyDelete
  15. Thank you for this blog! Thanks for sharing Information on Nodejs. I really Thanks to share the blog for such an beautiful Information.
    Nodejs Online Training

    ReplyDelete
  16. Nice blog! I read your blog. I really like all the points you mentioned are interesting and useful. Thanks for sharing this blog.
    Node Js Training Institute in Noida

    ReplyDelete
  17. Informative article, just what I was looking for. Read more about mean stack training in chennai from our website.

    ReplyDelete
  18. This comment has been removed by the author.

    ReplyDelete

  19. Wow such an amazing content keep it up. I have bookmarked your page to check out more informative content here.

    SASVBA Delhi provides extensive MERN STACK COURSE IN DELHI The extensive hands-on session, hosted by the MERN Stack Training Institute in Delhi, includes live projects and simulations.

    FOR MORE INFO:

    ReplyDelete
  20. Wow such an amazing content keep it up. I have bookmarked your page to check out more informative content here.

    SASVBA Provides Best Deep Learning Course in Delhi with Latest Development Environment and Frameworks. We keep Our Courses Up to Date with the Latest industrial trends. SASVBA Is One of the best training Deep Learning Institute in Delhi NCR Which Helps Students to Crack Interviews in Tech Giants. We train college students as well as school students.

    FOR MORE IMFO:

    ReplyDelete
  21. Thanks for the Valuable information.Really useful information. Thank you so much for sharing. It will help everyone.

    Power BI Institute in Delhi

    FOR MORE INFO

    ReplyDelete
  22. It is amazing and wonderful to visit your site. Thanks for sharing information; this is useful to us....

    VBA Macro Training In Delhi

    FOR MORE INFO:

    ReplyDelete