95

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?

Shadowfax
  • 1,567
  • 2
  • 13
  • 16

1 Answers1

282

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/11029354661

               Req/sec  Trans/sec
Nest-Express    15370   3.17MB  
Nest-Fastify    30001   4.38MB  
Express         17208   3.53MB  
Fastify         33578   4.87MB      

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)

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:

app.get('/', (req, res, next) => res.status(200).send('Hello world'));

Is unfair in this case, because it's not enough. When I cover these points, this is what I received (express 4.16.2):

Running 10s test @ http://localhost:3000
1024 connections

Stat         Avg    Stdev   Max
Latency (ms) 225.67 109.97  762
Req/Sec      4560   1034.78 5335
Bytes/Sec    990 kB 226 kB  1.18 MB

46k requests in 10s, 9.8 MB read

Additionally, Nest has to:

  • recognize whether a result is a Promise/Observable/plain value
  • 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):

Running 10s test @ http://localhost:3000
1024 connections

Stat         Avg    Stdev   Max
Latency (ms) 297.79 55.5    593
Req/Sec      3433.2 367.84  3649
Bytes/Sec    740 kB 81.9 kB 819 kB

34k requests in 10s, 7.41 MB read

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. The comparisons with Hello world means nothing, it's only a titbit :)

PS. I used autocannon library https://github.com/mcollina/autocannon

autocannon -c 1024 -t30 http://localhost:3000

Kamil Myśliwiec
  • 8,548
  • 2
  • 34
  • 33
  • 17
    @Cozzbie unless you use the Fastify adapter, as stated above. Then Nest is faster than Express. – jalooc May 29 '19 at 11:51
  • 19
    hi @Kamil, so this answer is from year 2018, and based on nest 4.5. Now we are in end of 2019 with nest 6.9.0, and at the same time we are having growing community which is eager to use nestjs, it would be very helpful if you guys keep updating this answer on frequently basis. At least once in 3 month?? – Kedar9444 Nov 13 '19 at 19:44
  • 7
    @Kedar9444 I don't doubt Kamil would love to see this updated regularly but as you must realize he is just one man. The purpose of the community is to fill in the voids. So, if you are interested in the benchmark and would like to showcase your work, contributions you are eagerly encuraged to do so. – EmilCataranciuc Mar 15 '20 at 16:44
  • 2
    Do you have benchmark updates this year ? – Ujjual May 10 '21 at 01:20
  • 1
    Also in 2021 is native Fastify app faster than Nest.js on Fastify. In our company we testet Nest.js with our API vs. native Fastify version of the same API. The native version is 42% faster. In my experience, the difference in real applications is even greater. But that also has to do with the fact that we can optimize the application structure better than with Nest.js. the possibilities are simply better without a given cluttered framework. – Alex Sedeke May 26 '21 at 15:01
  • How come that these numbers are fluctuating so much? See here for example: https://github.com/nestjs/nest/pull/7717/checks?check_run_id=3205746219 – Josh Aug 01 '21 at 17:46
  • 1
    Could you update your answer with 2021? I'm thinking of adopting Nestjs, but still not sure due to all these changing numbers. I would have chosen nest-fastify, but the docs say that I should use express instead since fastify does not support graphql playground, and using a non-stable release is out of the question. – Omar Dulaimi Oct 12 '21 at 14:30
  • I just just did test a simple query that returns a string with graphql and prisma in two scenarios: 1) default nestjs config 2) fastify and apollo server fastify. First scenario 13k req/10sec, Second scenario 40k/10s. So fastify is 3 times faster in my case. But having to use "apollo-server-fastify": "^3.0.0-alpha.3" means I will not be able to benefit of this performance gain – Omar Dulaimi Oct 12 '21 at 15:19
  • Just tested - fastify 3.26.0 vs. nest(fastify) 8.2.5 performance ... https://gist.github.com/ladisalves/f9b60d8b16f44c512ddd4070ef35026a results: fastify 64k requests, nestjs (fastify) 43k – xstefi Jan 20 '22 at 11:51
  • Is nest.js still a viable api solution or have others come forward too? I'm curious how the performance is, and if anything has come along that others are now using, that is more performant? – james emanon Mar 18 '23 at 07:52
  • So Nest is slower than Express – Marius Aug 05 '23 at 06:25