Home > Enterprise >  Requiring complete module vs requiring only required functions in NodeJS
Requiring complete module vs requiring only required functions in NodeJS

Time:02-19

Is there any performance difference when I require complete module vs when I require only the specific functions from the module in NodeJS?

CodePudding user response:

First off, let's assume you're talking about the difference between this:

 const myModule = require('myModule');
 myModule.doSomething();

and this:

 const { doSomething } = require('myModule');
 doSomething();

In both cases, you're loading and initializing the entire module. In the second case, you're only retaining one exported function, but the entire module has still been loaded and is in the module cache.

Is there any performance difference when I require complete module vs when I require only the specific functions from the module in NodeJS?

No difference.

  • Related