Skip to content

import('typescript') is 3x slower than require('typescript') #35574

Closed
@AviVahl

Description

@AviVahl
  • Version: 14.13.1
  • Platform: Linux. Fedora 32.
  • Subsystem: modules?

What steps will reproduce the bug?

mkdir slow-esm
cd slow-esm
npm init -y
npm i typescript

Now create two files:

a.js:

(async () => {
    const start = Date.now();
    require('typescript')
    console.log(`requiring typescript took ${Date.now() - start}ms`);
})()

a.mjs

(async () => {
    const start = Date.now();
    await import('typescript');
    console.log(`requiring typescript took ${Date.now() - start}ms`)
})()

node a.js outputs:
requiring typescript took 147ms

node a.mjs outputs:
requiring typescript took 440ms

How often does it reproduce? Is there a required condition?

The numbers change +-10ms, but the slowdown is pretty noticable.

What is the expected behavior?

Have a somewhat similar evaluation speed?

What do you see instead?

3x slowdown.

Additional information

I actually converted a commonjs app to native esm... the slowdown was clear, so I decided to make the naive check.

EDIT: Is it because typescript is a commonjs module? tokenization kicks in to find named exports? If so, is there a way to turn it off so I can check the speed without it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    esmIssues and PRs related to the ECMAScript Modules implementation.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions