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.
众所周知,require(...)方法是同步的,可能需要一些时间来执行。因此,建议在顶部加载所有需求。
I am using more than 15 modules. Now instead of writing multiple require statements, I am thinking of the following logic:
我使用超过15个模块。现在我没有编写多个require语句,而是考虑以下逻辑:
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?
你在写15个需求语句或任何动态机制吗?
thanks,
1 个解决方案
#1
As you all know that the require(...) method is sync and can take some time to execute.
众所周知,require(...)方法是同步的,可能需要一些时间来执行。
Why does it matter? Remember that the result of require()
is cached.
为什么这有关系?请记住,require()的结果是缓存的。
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
我使用超过15个模块
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.
没有大碍。同样,大多数应用程序都会在它们全部加载时加载。如果我需要一个需要文件的文件需要一个文件,那么它们都会立即加载,假设你的require语句在正常情况下排在最前面(或者在初始执行的任何地方)。
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.)
除非您仅在特定请求进入时要求,否则您无需担心。 (如果你这样做,我想你会有特殊的理由这样做。)
#1
As you all know that the require(...) method is sync and can take some time to execute.
众所周知,require(...)方法是同步的,可能需要一些时间来执行。
Why does it matter? Remember that the result of require()
is cached.
为什么这有关系?请记住,require()的结果是缓存的。
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
我使用超过15个模块
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.
没有大碍。同样,大多数应用程序都会在它们全部加载时加载。如果我需要一个需要文件的文件需要一个文件,那么它们都会立即加载,假设你的require语句在正常情况下排在最前面(或者在初始执行的任何地方)。
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.)
除非您仅在特定请求进入时要求,否则您无需担心。 (如果你这样做,我想你会有特殊的理由这样做。)