Skip to content

tniezurawski/nestjs-resolvefield-performance

Repository files navigation

Issue reproduction

  1. Set up the project
yarn install
  1. Start the server
yarn start:dev
  1. Go to the playground - http://localhost:3000/graphql
  2. Run the products query:
query Products {
  products {
    id
    direct
    resolved
    combined
    parent
  }
}

10 000 products will be returned. You can adjust the number in src/products/products.mock.ts file.

Notes about fields:

  • id and direct are passed directly from the REST response, no resolver involved
  • resolved field is being resolved through a resolver, but a very simple one. Just pointing to the REST field (1:1)
  • combined field has a resolver that concatenates two fields (think about it as "full name" that's created from first and last name fields)
  • parent same as resolved but uses @Parent() decorator
  1. Run the products query with just a single field case to understand its impact

My findings

Methodology

In each case, I run a simple query with id and "a field". Here's the k6 command I used:

k6 run --vus 1 --duration 30s --http-debug ./performance/--CASE--.js

direct

This is the baseline. 1353 requests in 30s:

image

resolved

resolved field is a 1:1 map to a field (from another service response or database). 582 requests in 30s. That's a 57% degradation in performance! Or in other words, not using a resolver is 2.3x faster.

image

parent

Same as resolved but also uses @Parent decorator. 346 requests in 30s. That's a ~75% drop in performance! Or in other words, the base is 3.9x faster than a resolver that happens to use the @Parent decorator.

image

Clinic

I used two cases here, and this time run as many requests as I could in 10 seconds to see if there were any discrepancies in functions being run.

# First terminal
clinic flame -- node ./dist/main.js

# Second terminal
k6 run --vus 1 --duration 10s --http-debug ./performance/direct.js

direct

The hottest function is willResolveField from @apollo.

image

resolved

This time, the hottest function is in NodeJS - processTicksAndRejections. That indicates a huge amount of asynchronous operations:

image

parent

Async queue seems to be even busier when using @Parent() decorator:

image

Working theory

I think that @ResolveField is automatically turning functions into async functions even if the developer didn't state that and is making very simple synchronous operations. In the experiments here, I took it to extreme and just returned the same field to show the difference but a case for simple synchronous operations has to exist.

Think about fullName that concatenates firstName and lastName or renaming some field or any other way of transforming the data.


Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications.

NPM Version Package License NPM Downloads CircleCI Coverage Discord Backers on Open Collective Sponsors on Open Collective Donate us Support us Follow us on Twitter

Description

Nest framework TypeScript starter repository.

Project setup

$ yarn install

Compile and run the project

# development
$ yarn run start

# watch mode
$ yarn run start:dev

# production mode
$ yarn run start:prod

Run tests

# unit tests
$ yarn run test

# e2e tests
$ yarn run test:e2e

# test coverage
$ yarn run test:cov

Deployment

When you're ready to deploy your NestJS application to production, there are some key steps you can take to ensure it runs as efficiently as possible. Check out the deployment documentation for more information.

If you are looking for a cloud-based platform to deploy your NestJS application, check out Mau, our official platform for deploying NestJS applications on AWS. Mau makes deployment straightforward and fast, requiring just a few simple steps:

$ yarn install -g mau
$ mau deploy

With Mau, you can deploy your application in just a few clicks, allowing you to focus on building features rather than managing infrastructure.

Resources

Check out a few resources that may come in handy when working with NestJS:

  • Visit the NestJS Documentation to learn more about the framework.
  • For questions and support, please visit our Discord channel.
  • To dive deeper and get more hands-on experience, check out our official video courses.
  • Deploy your application to AWS with the help of NestJS Mau in just a few clicks.
  • Visualize your application graph and interact with the NestJS application in real-time using NestJS Devtools.
  • Need help with your project (part-time to full-time)? Check out our official enterprise support.
  • To stay in the loop and get updates, follow us on X and LinkedIn.
  • Looking for a job, or have a job to offer? Check out our official Jobs board.

Support

Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.

Stay in touch

License

Nest is MIT licensed.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors