nodejs / nodejs/node

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

Open
#59,913 10 comments 11 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
122k
Forks
37.3k
Avg merge
4d 2h
Merged PRs (30d)
283

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked reproduction and compare import.mjs with require.cjs on Node.js v24.8.0 and v22.18.0 using the provided timing commands. Investigate the reported cjs-module-lexer overhead for the CommonJS typescript package; done means the performance difference is reproduced, explained, and any proposed scope is documented.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js, typescript
Domain
performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.