Nestjs Vs Expressjs

Posted on  by admin
··

6 min read

Modified7 months ago. I've just tested performance on a simple nest's controller, that returns text on a get request (no database).And the same simple GET controller (middleware) with express.

I used WRK tool to test performance. And as a result plain express is 2 x times faster than nestjs.Why is so much overhead created by nestjs? 22 gold badges1212 silver badges1515 bronze badges. 1515 bronze badges. UPDATE - 17.03.2020. We are now running benchmarks for every new PR. One of the latest benchmarks can be found here: https://github.com/nestjs/nest/runs/482105333. That means Nest + FastifyAdapter is now almost 2 times faster than express.

UPDATE - 22.09.2018. Benchmarks directory has been added to the repository: https://github.com/nestjs/nest/blob/master/benchmarks/all_output.txt (you can run benchmarks on your machine as well). UPDATE - 24.06.2018. Nest v5.0.0 supports fastify.

Fastify + Nest integration is even more performant than plain(!) express. The following list shows what Nest is doing in comparison to plain express route handler:. it surrounds your route handler body with try.catch blocks. it makes every route handler async.

it creates a global express router.

it creates a separated router for each controller. it binds error-handling middleware. it binds body-parser middleware (both json and extended urlencoded).

Express in use

All of the mentioned things reflect a real-world example (probably 99.9% express apps have to do this as well, it's unavoidable). It means that if you want to compare Express and Nest performance, you should at least cover above points. The comparison with the example below:. Is unfair in this case, because it's not enough. When I cover these points, this is what I received (express 4.16.2):. Additionally, Nest has to:. recognize whether a result is a Promise/Observable/plain value.

Features

based on the result type, use send() or json() (+1 condition). add 3 conditions (if statements) to check pipes, interceptors and guards. There's an output for Nest (4.5.8):. This implies that Nest performance is around 79% express (-21%). This is due to the reasons set out above, and moreover, because Nest is compatible with Node 6.11.x which means that it can't use async/await under the hood - it has to use generators. Which conclusion is to be drawn based on those stats? None, because we aren't used to creating applications that only returns plain strings without any asynchronous stuff.

Developer Experience

The comparisons with Hello world means nothing, it's only a titbit :). I used autocannon library https://github.com/mcollina/autocannon. 11 gold badge3030 silver badges3030 bronze badges. 3030 bronze badges. I'm planning with a small team to create an application which is a platform for restaurants. I'm on the backend almost alone currently. I'm going to use Node.js for that, and I'm very fond of TypeScript, and I worked before mostly with ExpressJS. The team may get bigger as the application becomes bigger and more successful, so I have the Scalability concern in mind now, and I was considering these options:1) Use Node+Express+Typescript2) Use Node+NestJs (which utilizes Typescript by default).

Application Structure

Option 2 is enticing to me because recently I came to love NestJS and it provides more scalability for the project and uses Typescript in the best way and uses Express under the hood.

Modules

Also I come from an Angular 2 background, which I think is the best frontend framework (my opinion, and I know React quite well), which makes Nest feel familiar to me because of the similarity between Nest and Angular.Option 1 on the other hand uses Express which is a minimalist framework, very popular one, but it doesn't provide the same scalability and brings decision fatigue about what to combine with it and may not utilize Typescript in the best way.

Controllers

Yet, on the other hand, it is flexible and it may be easier to manipulate things in different ways with it.Another very important thing is that it would be easier in my view to hire Node developers with skills in Express than NestJs. The majority of Node developers are much more familiar with JavaScript and Express.

Services

What is your advice and why? I would love to hear especially from developers who worked on both Express and Nest. I have experience using Express as a backend. I also use TypeScript in Express. However, my friend told me that NestJS is better in term of scalability. But it doesn’t really matter me because I try to organize the project structure as close as the clean architecture thing.

Dependency Injection

So my question is, except from the project structure thing, does NestJS provide any advantages over Express with TypeScript? As a personal preference, I also don’t like the concept of Angular. I tried to use NestJS but it’s super confusing and strict. I really need to follow the documentation and I started to ask myself “If I want to use X module, which NestJS may support but no documentation, how can I use it?”; For example, PassportJS, there is a documentation for that but only JWT and Basic Auth. I want to implement Facebook Auth, and Twitter Auth but can’t seem to find any documentation about that.

Bootstrapping an app using node has been often easy, because of the low complexity of creating a node backend, a little bit of express configuration and you're done.

Soon enough, you will find yourself asking the following questions:. Do I need to follow any kind of pattern? Worry about variable types? What kind of folder structure to use? The good thing about Node is that it makes you feel free when you have to take a decision about architecture, variables or folder structure. It is easy to start a writing an application, but hard to maintain it and be congruent about data typing. Use some kind of Javascript framework, there are many options out there, but for this post we are using NestJs.

Uses the good old MVC pattern;. Has a CLI to generate code for you;. Has typechecking, avoiding variable typing bugs. Has a learning curve, specially if you don't know Typescript very well and don't use the MVC pattern very often;.