最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - Profiling of import times in NodeJS application - Stack Overflow

programmeradmin3浏览0评论

Is there a way to track the duration of file imports in a Node.js application?

We have a TypeScript application running in VSCode, with the target set to ES2020 and the module format set to ESNext.

The application is launched using the following command:

{
      "name": "Run",
      "request": "launch",
      "type": "node",
      "args": [
        "--loader",
        "ts-node/esm",
        "--experimental-specifier-resolution=node",
        "--no-warnings=ExperimentalWarning",
        "--env-file=.env",
        "--env-file=.env-testnet-debug",
        "build/app.js",
      ],
      "cwd": "${workspaceRoot}",
      "internalConsoleOptions": "openOnSessionStart"
    },

However, when launching our application during development, there is a 5-10 second delay between pressing F5 in VSCode and seeing the first console.log output from app.ts/app.js.

The app.ts file contains standard imports for the rest of the application, such as:

import { App } from "./app/classApp";
...

we tried to re-write imports to dynamic way:

const { App } = await import("./app/classApp");

Then, the app starts immediately, and we can see that the classApp import is responsible for the 5-10 second delay.

We attempted to write a tool that automatically rewrites all imports to use await import() while collecting debug statistics. However, we encountered issues with TypeScript types when importing dynamically.

Is there an elegant solution to visualize the entire import tree, including both in-house and external libraries, so we can identify what is slowing down our app startup? Our goal is to mark certain (likely external) dependencies as dynamic imports, so they load during runtime instead of at startup.

Thanks for any advice

  • we tried to rewrite imports to dynamic await imports
  • we tried to search here on SO as same as ChatGPT to recommended solution but without success
  • there is no bundler used, just tsc for transpilation typescript to JS and than NodeJS is used to execute these .js files
发布评论

评论列表(0)

  1. 暂无评论