Categories
express javascript node.js

How expensive is it to require() something within an Express.js app?

I have an Express.js web app that is serving one of my domains. The app.js file looks like this:

var express = require('express');
var app = express();
// and so on…

I would like to use one of my own functions within the app.js file, so I thought I’d put the function in a separate file (as a module, i.e. module.exports = stuff) and then require it in the app.js file:

var myfunc = require('./path/to/myfunc');

However, I am worried about performance. Is there a significant performance penalty when requiring files inside an Express.js app? I guess this question boils down to how many times the app.js code is executed – once per HTTP request for my domain or only once during initialization, and whether or not the require() result is cached somehow between HTTP requests.