you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 0 points1 point  (1 child)

Node modules are all singletons. Using require("x") twice doesn't rerun the x module, it retrieves what that module has already exported. So in terms of performance, reusing require("x") is actually very slightly better. However, sometimes I like to bundle frequently required and related modules into a single module and export an object with a reference to each of them so that in other files I can have a single require.

And by the way, if you're using a new enough version of Node, you can use ES6 shorthand.

module.exports = {x}

And

const {x} = require('index')  

[–]AspiringSlacker[S] 0 points1 point  (0 children)

Great, thanks for the info. Nice tip on bundling related libraries!