Fabian
Fabian

Reputation: 177

Express.js router require directly or inside a variable?

I have a express.js performance question. I have my server.js where all the routes are defined and the child routes are imported like so:

const ROUTE__FOO = require('./routes/foo')
const ROUTE__BAR = require('./routes/bar')

app.use('/api/foo', ROUTE__FOO)
app.use('/api/bar', ROUTE__BAR)

So my question is: Is it better / faster to first require the routes inside a variable and then assign this variable to the express.js app.use function? Or can I also do this:

app.use('/api/foo', require('./routes/foo'))
app.use('/api/bar', require('./routes/bar'))

Would there be any problems? I tried to find out what's better, but I couldn't get anything out for this specific problem.

Upvotes: 0

Views: 23

Answers (1)

Aron
Aron

Reputation: 9246

It doesn't make any difference. For readability it's probably better to put the requires at the top of the file, but performance wise it is the same.

Upvotes: 1

Related Questions