nodejs / nodejs/node

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

オープン
#59,913 コメント 10 件 リアクション 11 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

主要言語
JavaScript
スター
122k
フォーク
37.3k
平均マージ
4日 2時間
マージ済み PR(30日)
283

説明

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. リポジトリをフォークし、ブランチを切って変更します。
  4. 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 を短くまとめたダイジェスト。