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