node.js - Node - Is it more memory efficient to require module inside setInterval versus outside? -
i have node file i'm using service file/daemon on linux server.
something this:
const m = require('./local-module') setinterval(m, duration)
that module exports function, contains 'global' variables, i've noticed how variables same each time function called, makes sense me.
that has me wondering whether like:
setinterval(() => { require('./local-module')() }), duration)
is more memory efficient? doing 1 way have benefits on other?
calls require()
cached , returning same instance. whether
var thing = require('thing'); thing.stuff()
or
require('thing').stuff()
it's same instance of "thing" each time.
for readability , convention, putting requires @ top more common. however, since loads require modules when file loads (on app start), might not testing paths or mocking or initialization sequences. doing require()
in middle of code loads module when called, can lazy it, helpful.
Comments
Post a Comment