Is NodeJS faster than…
I have an opportunity to develop client websites in various programming languages. Years ago I started with PHP, and I was using Ruby for some time… Later I was working with JSP and ColdFusion which is my primary programming language right now. ColdFusion is not very popular and is considered outdated, but there are still users who want their websites to be updated rather than rewritten in the new language. Recently I started my journey with NodeJS, and I was wondering if this is a good choice. The main advantage and the reason why I chose NodeJS was that I already used JavaScript a lot of writing frontend of the web pages. I thought that I will be able to jump in quickly and that I will start coding in an already known language. I was also considering Python because I’m also using it – for automating tasks mostly, not for larger projects.
Each framework is advertised as fast and reliable – the same with programming languages. I noticed that NodeJS is considered to be a fast and effective solution for current programmers world problems. There are various benchmarks and tests. You can read articles comparing Python, NodeJS and other languages. In some of them you will read that Python is the fastest. You can find ones where NodeJS beats all others… What I found problematic is the fact that these tests have nothing in common with my everyday usage of programming languages. What I do for most of the time is writing the pages that are interacting with the user, retrieving the data from the database and serving various types of content.
Comparisons
Basic ColdFusion page can take 15-30 ms to respond with the content. It also takes some time to communicate between the browser and the server. Typically it is not a single request but a series of packets sent between the parties. All of these takes some time. When I started my work with NodeJS, I noticed that the same simple page generated by the Express framework takes 2-3 ms to respond. I was surprised and thrilled – I found the software which is much faster and will be able to handle more requests at the same time. I thought that NodeJS is an excellent solution for my future projects.
The problem is that my pages are not static. They are not displaying the same information for all users. They are interacting with the databases and other data sources. I had to add session management, and I had to add cookies handling to manage sessions properly. I expect that there will be multiple sessions at the same time and I have to store session data somewhere, so I added database layer to my stack. Once all these pieces were in place, my simple page suddenly started to take 15-25 ms to respond. Cookies have to be sent and received, session data have to be retrieved from the database, variables are handled, and there is much more code to be executed.
ColdFusion and PHP are performing the same tasks in the background – even the simplest page is generating session information which is handled server side. Cookies are prepared and exchanged. The server is performing much more operations that we assume. When it comes to more complex pages, in my case the most of the time is taken by the database queries and exchanging the data between the database server and the web server. In most cases there are no complex transformations performed on the retrieved data, they are present to the client. This means that no matter what language is used, I will most likely wait for the same amount of time to grab the data I need.
Conclusions
I’m not telling that NodeJS is not fast. What I want to show is that there are various factors you should take into consideration when thinking of the speed of your website. In many cases, we found that the problems with the site performance were caused by the database queries or a general high load of the server (caused for example by the static content requests – images, videos). If you want to change your programming language only because of the performance issues, you have to develop your metrics and your tests to measure if the performance gain you will achieve is worth the work with the project rewriting. In all the cases I had so far, I was able to find better solutions using existing technology stack.
On the other hand, when it comes to the entirely new pieces of code, NodeJS can be a high-speed and reliable solution. I started to write simple webservices (or microservices) in NodeJS, and this is the solution I was looking for. Because of the possibility of using stateful and stateless variables, I can “cache” the frequently used data and serve them almost immediately. The server is running , so there is no warmup at the beginning of the request and no shutdown at the end. If there is no need to handle cookies or session, I can skip these modules and work only with what I need. This means smaller overhead and less code to process.
I found that there is no golden mean in most of my work when it comes to performance. Tests performed by others are a good start, but one way or another I have to test my case and decide what is the best solution in my case. Sometimes “the best” doesn’t mean the most performant solution, but also the one which is cost effective. You have to choose on your own.