Search code examples
node.jshttpservernestjsfastify

Result perf node using mongo nestJs or fastify


I have made two projects, one using nestJS/mongoDB and the other Fastify/MongoDB in order to compare performance of both frameworks. I made a simple get resource API querying a mongo document having ~400Kb. Both projects route responds with the JSON document over 300ms. Can someone explain to me from where comes the extra 280ms as mongo responds no more than 20ms.

It's not a matter of the framework they both responds with the same time. Is it the compression done by the API, is it the time taken by node server

When I profile the code, mongo responds no more than 20ms, in both case, with the document that is immediately returned by the get handler. There is no boilerplate code or extra logging, just plain HttpServer.

in Fastify I'm doing this way

{
method: 'GET',
url: '/api/trees/:id',
handler: async (req, reply) => {
  try {
    const id = req.params.id
    const tree = await TaxonomiesTrees.findById(id)
    return tree.data
  } catch (err) {
    throw boom.boomify(err)
  }
 }
}

in NestJS like this

 @Get(':id')
 async getTree(@Param('id') code: string) {
  const result = await this.treesCollection.findOne({id});
  return result.data
 }

Solution

  • I found a response to my question in case, that seems somehow logic. As I doubted It's related to the time taken by the webserver to interpret the into bytes the result from the controller. Known as the TTBF a measurement of time taken from the http request to the first byte sent to the client.