简体   繁体   English

在node.js中加载延迟

[英]Lazy loading in node.js

I was wondering if using require() in node.js was the equivalent to lazy loading? 我想知道在node.js中使用require()是否相当于延迟加载?

For example if I had a function that required a specific node.js package that wasn't needed anywhere else in my code am I best to use require() inside of that function to include the needed package only when that function is called. 例如,如果我有一个函数需要一个特定的node.js包,而我的代码中的任何其他位置都不需要,我最好在该函数内部使用require()来仅在调用该函数时包含所需的包。

I'm also unsure if this will provide any performance improvements given my lack of understanding around the node.js architecture? 鉴于我对node.js架构缺乏了解,我也不确定这是否能提供任何性能改进? I presume it will use less memory per connection to my server. 我认为它将为我的服务器每个连接使用更少的内存。 However will it increase I/O to the disk when it has to read the package, or will this be a one off to get it in memory? 但是,当它必须读取包时会增加磁盘的I / O,还是一次性将它放入内存中?

If this is the case how far should I take this, should I be trying to write node.js packages for as much code as I can? 如果是这种情况我应该采取多少措施,我应该尽可能多地尝试编写node.js软件包吗?

require() is on-demand loading. require()是按需加载。 Once a module has been loaded it won't be reloaded if the require() call is run again. 加载模块后,如果再次运行require()调用,则不会重新加载。 By putting it inside a function instead of your top level module code, you can delay its loading or potentially avoid it if you never actually invoke that function. 通过将其放在函数而不是顶级模块代码中,您可以延迟其加载,或者如果您从未实际调用该函数,则可能会避免加载。 However, require() is synchronous and loads the module from disk so best practice is to load any modules you need at application start before your application starts serving requests which then ensures that only asynchronous IO happens while your application is operational. 但是, require()是同步的并从磁盘加载模块,因此最佳做法是在应用程序开始提供请求之前加载应用程序启动时所需的任何模块,然后确保在应用程序运行时仅发生异步IO。

Node is single threaded so the memory footprint of loading a module is not per-connection, it's per-process. 节点是单线程的,因此加载模块的内存占用量不是每个连接,而是每个进程。 Loading a module is a one-off to get it into memory. 加载模块是一次性进入内存。

Just stick with the convention here and require the modules you need at the top level scope of your app before you start processing requests. 在开始处理请求之前,请坚持使用此约定并在应用程序的顶级范围内需要所需的模块。 I think this is a case of, if you have to ask whether you need to write your code in an unusual way, you don't need to write your code in an unusual way. 我认为这是一个例子,如果您不得不问是否需要以不寻常的方式编写代码,则不需要以不寻常的方式编写代码。

If you want to lazy load modules, its now possible with ES6 (Node v6) 如果你想延迟加载模块,现在可以使用ES6(Node v6)

Edit: This will not work if you need to access properties of require (like require.cache). 编辑:如果您需要访问require属性(如require.cache),则无效。

module.js module.js

console.log('Module was loaded')
exports.d=3

main.js main.js

var _require = require;
var require = function (moduleName) {
    var module;
    return new Proxy(function () {
        if (!module) {
            module = _require(moduleName)
        }
        return module.apply(this, arguments)
    }, {
        get: function (target, name) {
            if (!module) {
                module = _require(moduleName)
            }
            return module[name];
        }
    })
};

console.log('Before require');
var a = require('./module')
console.log('After require');
console.log(a.d)
console.log('After log module');

output 产量

Before require
After require
Module was loaded
3
After log module

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM