`import "typescript"` slower than `require("typescript")` due to `cjs-module-lexer` overhead
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.
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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