Search code examples
node.jstypescriptmemorynestjsts-node

What is the best type of import to use to reduce memory usage?


So, Hi. I recently wondered what type of import is best for my microservices on Nestjs and ts-node. With using version 20 of nodejs and version 10 of nestjs, my microservices started consuming a lot of memory. The consumption goes up to 300mb for a single pod. I decided to try to change the way I import my dependencies when implementing my new microservice. I changed imports only in main.ts and already my microservice started to consume 10mb of memory.

What I used before

import { NestFastifyApplication, FastifyAdapter } from '@nestjs/platform-fastify';

What I decided to try

import { NestFastifyApplication } from '@nestjs/platform-fastify/interfaces/nest-fastify-application.interface';
import { FastifyAdapter } from '@nestjs/platform-fastify/adapters/fastify-adapter';

Has anyone done tests on using different types of imports? I will share my tests next week. I have enough other tasks to do right now.


Solution

  • I've done my research and tests. I came to the result that it slightly affects the amount of memory consumed. I managed to reduce memory consumption by 20-45 mb depending on the number of connected libraries. But this effect is only for those who use TS code without compiling to JS.