nodejs / nodejs/node

`import "typescript"` slower than `require("typescript")` due to `cjs-module-lexer` overhead

未关闭
#59,913 10 条评论 11 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
JavaScript
星标
122k
派生
37.4k
平均合并
4 天 3 小时
30 天内合并 PR
272

描述

Version
  • Node.js v24.8.0
  • Node.js v22.18.0
Platform

Observed on macOS (x64), but likely reproducible on other platforms.

Description

When loading typescript, using import is ~100ms slower than require.

This seems to come from the cjs-module-lexer step Node.js runs for CommonJS packages. Since typescript itself is CommonJS, require avoids the overhead and is noticeably faster.

Image
Reproduction

https://github.com/chenjiahan/nodejs-repro-require-import-typescript

// import.mjs
import typescript from "typescript";

console.log(typescript);

// require.cjs
const typescript = require("typescript");

console.log(typescript);

Example results (macOS, Node.js v24.8.0):

# ~200ms
time node ./import.mjs

# ~100ms
time node ./require.cjs
Ref

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从链接的复现开始,使用提供的计时命令在 Node.js v24.8.0 和 v22.18.0 上比较 import.mjs 与 require.cjs。调查针对 CommonJS 包 typescript 报告的 cjs-module-lexer 开销;当性能差异得到复现和解释,并且任何拟议的范围都得到记录时,即视为完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, node.js, typescript
领域
performance
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
冷清
描述清晰度
需要澄清
新手友好度
42/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。