2

As you all know that the require(...) method is sync and can take some time to execute. Thus, it is advisable to load all require on the top.

I am using more than 15 modules. Now instead of writing multiple require statements, I am thinking of the following logic:

foreach(var module in node_modules) 
 GLOBAL.app.npm[module] = require(module);

The question that I have is how you are handling the same?

Are you writing 15 require statements or any dynamic mechanism?

thanks,

codebased
  • 6,945
  • 9
  • 50
  • 84
  • If you're using modules the way they are intended, you won't be just loading a module. Instead, you'll be running a constructor and saving the result of that or assigning just a property from the module to a specific local variable. You can't do any of this in an automated fashion. Put the actual `require()` statements in your code that you code needs. That's the best way to document what modules you depend on and to save a reference to only the things you need from the module. And, none of this has anything to do with the fact they run sync. That won't change. – jfriend00 May 03 '15 at 07:55
  • What problem are you really trying to solve? Typing of a number `require()` statements? You aren't going to solve the sync issue unless you write your own module loader. – jfriend00 May 03 '15 at 07:56

1 Answers1

2

As you all know that the require(...) method is sync and can take some time to execute.

Why does it matter? Remember that the result of require() is cached.

This isn't really an issue in practice unless you are doing something strange in your application. And again, you don't have to put it in global, due to the caching. Just require once anywhere that runs up front and you're good to go.

I am using more than 15 modules

Not a big deal. Again, most applications are going to load when they all load anyway. If I require a file that requires a file that requires a file, they're all loaded immediately, assuming your require statements are up top (or anywhere in that initial execution really) like normal.

Unless you are only requiring when a specific request comes in, then you have nothing to worry about. (And if you are doing that, I'd imagine you would have a special reason to do so.)

Brad
  • 159,648
  • 54
  • 349
  • 530
  • thanks, I want to put into GLOBAL so that I don't have to require everywhere, i.e. outside of main file. My question is not to write 15+ require(...) as stated "Now instead of writing multiple require statements, ..." – codebased May 03 '15 at 05:28
  • @codebased No need for global... have one file that requires the others. Then you don't pollute your global space. See also: http://stackoverflow.com/a/5365577/362536 And your question isn't clear if you really are just trying to type less, because you specifically ask about reducing load time. – Brad May 03 '15 at 05:29
  • Reducing "number of require statements" in nodejs app ? not clear? – codebased May 03 '15 at 05:35
  • @codebased That's right. A proper answer takes into account *why* you want to do this, and I don't make it a habit to assume someone is going to wreck their global variable space out of laziness. Especially when your first sentence says, "As you all know that the require(...) method is sync and can take some time to execute. Thus, it is advisable to load all require on the top." As it reads to me anyway, your question is about optimization and nothing more. In either case, I proposed a solution to you. – Brad May 03 '15 at 05:37