node.jsnode-moduleses6-modulescommonjs

Is there any performance difference in ES modules vs Common JS modules in NodeJS servers?


Is there any performance advantage or disadvantage of using ES modules over CommonJS modules in a nodejs backend project? I'm asking this because I see a lot of people using CJS still even though newer versions of NodeJS support ES modules fully.


Solution

  • An import statement can reference an ES module or a CommonJS module. import statements are permitted only in ES modules, but dynamic import() expressions are supported in CommonJS for loading ES modules.

    When importing CommonJS modules, the module.exports object is provided as the default export. Named exports may be available, provided by static analysis as a convenience for better ecosystem compatibility.

    The major difference in CommonJS and ES module is of synchronous and asynchronous nature, which might affect performance: