Overview
I've done some reading on JavaScript memory management in the past, and am aware of the issue with circular DOM references, etc.
However I'm still a little bit uncomfortable as this translates to a server-side JavaScript environment such as node.js, and more specifically an API written on express.
Take this sample file (lets call it server.js)
var npm_moduleA = require('npmA')({ someInitArg : 'blah' }),
app = express.createServer();
app.get('/api/foo', function (req, res) {
var result = npm_moduleA.doSomething();
res.send(result);
});
app.get('/api/bar', function (req, res) {
var npm_moduleB = require('npmB')({ someInitArg : 'blah' }),
result = npm_moduleB.doSomethingElse();
res.send(result);
});
Questions (assuming this is a high-load site)
What is the lifecycle of
npm_moduleA
? It gets created at the moment that the server starts, but when (if at all does GC kick in against it) - I'm guessing that it never gets touched because its in the global scope?In '/api/bar/', should
npm_moduleB
be deleted after each request? Or should this be left to the GC alone.Is the global instantiation of
npm_moduleA
significantly more efficient than repetative the instantiation (and possible deletion) ofnpm_moduleB
?