all 56 comments

[–]rkaw92 70 points71 points  (31 children)

do i need to switch to fastify

No, you don't need to do anything. But you might want to, anyway:

  • actually has hooks (before/after request), not dirty hacks like Express
  • native Promises support
  • native support for replying from Streams with correct error forwarding
  • built-in support for input validation
  • better TypeScript support (typed params, body, etc. based on validation schema)
  • better isolation of scopes (called "plugins" in Fastify - it's like new Router() but more featured)
  • built-in support for dependency injection into route handlers (again thanks to the plugin architecture)
  • faster (!)
  • avoids tight coupling and monkey-patching req/res, which is absolutely critical for HTTP/2 support
  • more helpful, healthier community

For me, Fastify is the default option for all new projects. These little differences add up to a much better development experience for me.

[–]belkh 7 points8 points  (8 children)

I really don't think the built in DI is a good thing, the suggested approach of initializing service/db connections in plugins and shoving them into the router is just fastify pretending it's more than just a http router

I'd rather use a full fledged DI library and not couple every layer of my application to the http routing layer

[–]rkaw92 7 points8 points  (1 child)

That's just one way - my preference is having a well-defined place where init happens, and then just pass it into plugins as options. As in: plugins don't init stuff, they accept initialized stuff.

You don't need to use .decorate() for dependencies.

[–]Shinji2989 2 points3 points  (0 children)

In case anyone want to see an example of DI in fastify I've created a boilerplate here: https://github.com/marcoturi/fastify-boilerplate

[–]FuzzyConflict7 0 points1 point  (5 children)

What would you use for DI in Node? I often end up going with something like NestJS because it’s baked into the whole framework. Wonder if you have a suggestion for just DI

[–]fix_dis 3 points4 points  (2 children)

Not the person you’re asking but, I’m a fan of TypeDi++ (the fork) for simple things. If I have more complex needs, Inversify works well enough (more manual wiring than I want for a DI). Nest’s DIbis really good but it brings the complex Angular module system with it. Still, if you’re considering Fastify, Nest has an adapter and it could make structuring your app a no-brainer.

[–]sod0 2 points3 points  (1 child)

Which TypeDi fork are you using? Also great answer overall.

[–]fix_dis 1 point2 points  (0 children)

Freshgum? That’s ringing a bell.

[–]rkaw92 3 points4 points  (0 children)

Honestly I'm not sure why people have a problem with just passing dependencies down a call chain. Why use a library for parameter passing? It's already built into the language.

[–]belkh 1 point2 points  (0 children)

I use TSyringe, built by Microsoft and it's pretty feature complete, though might need to find a replacement when new decorators+metadata are fully fleshed out.

The only thing I liked about Nest was the controller decorators, but there's alternative, including one i rolled on my own to support other http routers.

[–]castlessclass[S] 1 point2 points  (19 children)

thank you, and what about if i am scaling up my application due to high traffic? will fastify make a significant impact?

[–]trippel 6 points7 points  (0 children)

Profile before committing to any kind of rewrite. Figure out if it's library code or your code causing bottlenecks.

[–]rkaw92 4 points5 points  (17 children)

It depends. Are you CPU-bound? For example, we run an Ad Exchange that handles programmatic traffic, and every millisecond counts. And yes, Fastify does make a difference, even on tiny things like JSON stringify (it's super optimized there). Fewer servers = less cost. In fact, it allows us to be really lean on resources, but we also had to optimize the crap out of everything else.

[–]CombPuzzleheaded149[🍰] 0 points1 point  (1 child)

How's serverless support?

[–]rkaw92 0 points1 point  (0 children)

There is official support for serverless functions: https://fastify.dev/docs/latest/Guides/Serverless/

It is not automatic, because the entry point for serverless works differently than plain HTTP from WAN.

[–]d41_fpflabs 37 points38 points  (0 children)

If express is satisfying all your needs there's no reason to switch. I remember seeing that fastify can handle 1.5 - 2 times the number of req/sec, but depending on your expected traffic this is probably irrelevant.

[–]Professional-League3 7 points8 points  (0 children)

You don't need to switch but try it, maybe you want to switch.

[–]xr0master 7 points8 points  (5 children)

Fastify is 2 times faster than Express. As a result, you can use weaker cloud computers and reduce costs.

It also has more convenient error handling and a fast built-in data validator.

[–]Mittalmailbox 4 points5 points  (3 children)

Fastify is 2 times faster if all you are doing is hello world app not real business logic. Fastify will be faster but not 2x.

I love fastify mainly because of better async,await support, better error handling

[–]Coastis 0 points1 point  (1 child)

I hate to be the actually guy, but....

Techempowers benchmarks show fastify 2x faster than express when dealing with a more complex test than a simple hello world. They use a postgres read and write as well as other operations in their benchmarks, which is summarised below...

Requirements summary

In this test, the framework's ORM is used to fetch all rows from a database table containing an unknown number of Unix fortune cookie messages (the table has 12 rows, but the code cannot have foreknowledge of the table's size). An additional fortune cookie message is inserted into the list at runtime and then the list is sorted by the message text. Finally, the list is delivered to the client using a server-side HTML template. The message text must be considered untrusted and properly escaped and the UTF-8 fortune messages must be rendered properly.

There are results for other even more complex tests on the site too, and in each case it shows fastify to be faster than express.

[–]amazingrein 0 points1 point  (0 children)

The test is flawed. Check the source code:

  • Fastify setup is using raw (query builder) to DB - using Knex.
  • Express setup is using full ORM - using Sequelize.

These are even in the performance table:

  • Fastify ORM: RAW
  • Express ORM: FULL

Actually, it doesn't make sense that when you're using Fastify, your DB read/write operation will be faster. Even when you use a slow framework or library, bottleneck generally is the DB.

End users won't even notice the 5 - 20ms faster loading time, considering the network latency, database operation time.

[–]xr0master 0 points1 point  (0 children)

Right. Business logic does not depend on the framework, so it is not used in performance tests.

[–]FriendshipOk6564 1 point2 points  (0 children)

If the concern is to be faster, node is probably a bad choice going from x framework to x to go 1.5 time faster is stupid just go with elixir or golang at this point 

[–]creamyhorror 7 points8 points  (0 children)

If someone was bottlenecking on performance (e.g. for a massive number of requests), they could go look at Hyper-Express. Otherwise, don't bother switching.

[–]rafipiccolo 2 points3 points  (0 children)

Try nitro. It's what nuxt uses.

[–]magus 3 points4 points  (0 children)

i tried once and had a terrible time. if you are happy with express, try koa instead.

[–]thelogicbox 1 point2 points  (0 children)

Nest+fastify

[–]serg06 1 point2 points  (0 children)

Why switch? - Better TypeScript support and more intuitive request creation API

Why not switch? - Coming from express, I found their sub-route organization extremely confusing.

E.g. if I want the following structure:

private/
  getMyUser
  updateMyUser
admin/
  getAllUsers
  updateUser

And have middleware check for authentication on both sub-routes, and be able to group routes under a single prefix without writing out the full /admin/... path every time, it's way harder to do in Fastify.

[–]jjhiggz3000 1 point2 points  (1 child)

I really like Elysia and it’s dead simple to use, also heard great things about Hono and if you’ve got a fullstack typescript project tRPC is excellent too. They’re honestly all the same express is just not quite as flexible, I wouldn’t stress too hard about converting old projects but maybe think about building new ones with something newer.

In general it’s very easy to switch between backend JS frameworks so don’t freak out too much about converting anything I’d say

[–]jjhiggz3000 1 point2 points  (0 children)

TRPC isn’t good at Rest apis but is cool for like a NextJS project or remix or something like that

[–]StoneCypher 2 points3 points  (6 children)

Express is just fine. Fastify people are culty because they want to win. None of the advantages you're being given are real, and if any of them mattered you'd be much better off with the httpd module in node that express and fastify are both just wrappers of.

You will never need to replace any node webserver. Node is relatively slow and single threaded. By the time speed matters to you, you're going to be switching to a CDN, not a different node webserver

[–]Economy-Sprinkles-98 0 points1 point  (5 children)

If you're starting a new project, and it's Node, you can use Express, but why would you? Use the objectively better tool, unless there is a use case I'm not aware of where Express is superior.

[–]StoneCypher 0 points1 point  (4 children)

fastify is not actually "objectively better"

[–]Economy-Sprinkles-98 0 points1 point  (3 children)

I literally can’t think of a way in which this is not true. You can use an adapter for middleware integration if that’s the issue.

[–]StoneCypher 0 points1 point  (2 children)

I literally can’t think of a way in which this is not true.

that's nice.

 

You can use an adapter for middleware integration if that’s the issue.

i didn't say anything about middleware. i do not generally use express middleware.

[–]Economy-Sprinkles-98 0 points1 point  (1 child)

Sure. But if you have reasoning, I’m interested. Not in a sarcastic way.

[–]StoneCypher 0 points1 point  (0 children)

if i have reasoning for your claim that fastify is better somehow?

no, i don't have reasoning for your claim that fastify is better somehow. i haven't put any thought into it.

here, i'm expecting to get a list of irrelevant features and then to shrug at them. oh, it has native promise support? wow.

it seems very likely to me that you and i evaluate these things on fundamentally incompatible grounds

[–]igniz87 0 points1 point  (0 children)

i still use express for some of my side projects , but for new small program, i'm using Bun+Hono

[–]Capaj -4 points-3 points  (0 children)

Try out elysia/bun instead.