9

I am working with a project that still transpiles TS into commonJS for running on Node. Now that node has started to toward ES Modules, is there a performance advantage to migrating away from commonjs? Or is the difference literally superficial?

I know in previous versions native ESM was not supported so the older answers and other questions seem to be outdated. Because of this I can't seem to find a good answer.

Jackie
  • 21,969
  • 32
  • 147
  • 289
  • I would really like to know this too – HumbleCoder Oct 14 '21 at 06:24
  • Given the more readable syntax, ES modules offer better developer performance. – Bergi Jan 04 '22 at 13:31
  • @Bergi please realise that your comment does not relate to the question. They use TypeScript -- hence, they always write ES Modules only. The question is whether to tune the transpiler to output CommonJS or ES Modules. It does not affect the developer performance. – enanone Feb 05 '23 at 11:10

1 Answers1

1

CommonJS loads modules synchronously, ES modules are asynchronous.

One of the limitations of using require() is that it loads modules synchronously. This means that modules are loaded and processed one by one.

As you might have guessed, this can pose a few performance issues for large-scale applications that hundreds of modules. In such a case, import might outperform require() based on its asynchronous behavior.

However, the synchronous nature of require() might not be much of a problem for a small-scale application using a couple of modules.

The above text is quoted directly from https://blog.logrocket.com/commonjs-vs-es-modules-node-js/

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
Algo7
  • 2,122
  • 1
  • 8
  • 19