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

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

c# SetCompatibleTextRenderingDefault must be called before the first -

Laravel mail error `Swift_TransportException in StreamBuffer.php line 269: Connection could not be established with host smtp.gmail.com [ #0]` -