最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Does NodeJS reuse already loaded modules and share it's values? - Stack Overflow

programmeradmin2浏览0评论

I'm starting to build my project in NodeJS and was wondering

  • Does NodeJS reuse already loaded modules for modules requiring the same module?
  • Do the modules keep the same "state" across modules that require them?
  • What's the catch if either or both happen? Am staring in the face an issue similar to loops and setTimeout and async code?

Currently I have tested with 4 files

  • mon.js

    var i = 0;
    
    function add(v){i += v;}
    function view(){console.log(i);}
    
    module.exports = {
        add : add,
        view : view
    }
    
  • a.js and b.js

    exportsmon = require('./mon.js');
    
  • server.js

    var a = require('./a.js'),
        b = require('./b.js');
    
    function start(){
        http.createServer(function (req, res) {
    
            amon.add(2);
            amon.view();
    
            bmon.add(4);
            bmon.view();
    
            amon.view();
    
    
            res.writeHead(200, {'Content-Type': 'text/plain'});
            res.end('Hello World\n');
        }).listen(config.port, config.address || '127.0.0.1');
        console.log('Server running');
    }
    

The result gives me a suggestive result that it does:

2  - view() via a.js on favicon request
6  - view() via b.js on favicon request
6  - view() via a.js on favicon request
8  - view() via a.js
12 - view() via b.js
12 - view() via a.js

It seems that it does seem to share the module even if it is required by two separate modules, and even keeps state across modules and across requests

I'm starting to build my project in NodeJS and was wondering

  • Does NodeJS reuse already loaded modules for modules requiring the same module?
  • Do the modules keep the same "state" across modules that require them?
  • What's the catch if either or both happen? Am staring in the face an issue similar to loops and setTimeout and async code?

Currently I have tested with 4 files

  • mon.js

    var i = 0;
    
    function add(v){i += v;}
    function view(){console.log(i);}
    
    module.exports = {
        add : add,
        view : view
    }
    
  • a.js and b.js

    exports.mon = require('./mon.js');
    
  • server.js

    var a = require('./a.js'),
        b = require('./b.js');
    
    function start(){
        http.createServer(function (req, res) {
    
            a.mon.add(2);
            a.mon.view();
    
            b.mon.add(4);
            b.mon.view();
    
            a.mon.view();
    
    
            res.writeHead(200, {'Content-Type': 'text/plain'});
            res.end('Hello World\n');
        }).listen(config.port, config.address || '127.0.0.1');
        console.log('Server running');
    }
    

The result gives me a suggestive result that it does:

2  - view() via a.js on favicon request
6  - view() via b.js on favicon request
6  - view() via a.js on favicon request
8  - view() via a.js
12 - view() via b.js
12 - view() via a.js

It seems that it does seem to share the module even if it is required by two separate modules, and even keeps state across modules and across requests

Share Improve this question edited Aug 21, 2012 at 11:30 Joseph asked Aug 21, 2012 at 11:19 JosephJoseph 120k30 gold badges184 silver badges237 bronze badges 3
  • Where do foo and bar e from? – Amberlamps Commented Aug 21, 2012 at 11:22
  • @Amberlamps edited. I used foo and bar in my code. – Joseph Commented Aug 21, 2012 at 11:25
  • yes, the loaded modules ar cached, their state to. – Stoia Alex Commented Aug 21, 2012 at 11:26
Add a ment  | 

2 Answers 2

Reset to default 6

Node.js caches modules for obvious performance reasons.

Statement from the Node.js website:

Modules are cached after the first time they are loaded. This means (among other things) that every call to require('foo') will get exactly the same object returned, if it would resolve to the same file.

Multiple calls to require('foo') may not cause the module code to be executed multiple times. This is an important feature. With it, "partially done" objects can be returned, thus allowing transitive dependencies to be loaded even when they would cause cycles. If you want to have a module execute code multiple times, then export a function, and call that function.

Yes, all modules are cached after the first time they are loaded. You can read more about module caching in official node.js docs.

发布评论

评论列表(0)

  1. 暂无评论