Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 580
Šime Vidas Avatar asked Jan 01 '14 23:01

Šime Vidas


People also ask

What does next () do in Express?

The next function is a function in the Express router which, when invoked, executes the middleware succeeding the current middleware.

What is App Express ()?

Express is a minimal and flexible Node.js web application framework that provides a robust set of features to develop web and mobile applications. It facilitates the rapid development of Node based Web applications.

Is Express JS good for production?

It's fast, unopinionated, and has a large community behind it. It is easy to learn and also has a lot of modules and middleware available for use. Express is used by big names like Accenture, IBM, and Uber, which means it's also great in a production environment.

What is req and res in Express?

The req object represents the HTTP request and has properties for the request query string, parameters, body, and HTTP headers. The res object represents the HTTP response that an Express app sends when it gets an HTTP request. In our case, we are sending a text Hello World whenever a request is made to the route / .


1 Answers

Code you require is fetched and executed a single time. After that, you get back whatever was in module.exports, or exports.

Whenever a second call to require requests what is at the same location, it gets the same reference to module.exports, or exports.

In your particular case, code required while bootstrapping the application is required once, while bootstrapping.

Then, whenever a request requires something that hasn't been required yet, it gets fetched and executed. Every single time after that, require calls will just fetch the cached data.

Remember how node is single threaded? That's why if you try to require a file that doesn't really exist, you'll bring the entire process down.

Update

Answering Šime's question, about "when" require calls are executed.

require calls are executed as they are met when following the code, there's no pre-processing or any fancy magic like that. Everything that's not part of a request is effectively "shared" by the whole process, if it has access to that scope.

Let's look at an example. Suppose this is your app.js, and I'm ignoring all the irrelevant express bootstrapping code, for brevity.

var a = require('./foo.js'); // [1]

app.get('/thing', function (req, res, next) {
    next(); // [2]
});

app.get('/thing', function (req, res, next) {
    res.end();
});

app.get('/foo', function (req, res, next) {
    res.end(a); // [3]
});

[1] foo is fetched immediately, as are any things foo requires, module.exports gets assigned to a.

[2] when this callback is invoked, we just tell express, do nothing, skip me and go to the next middleware.

[3] assuming a has some string assigned to module.exports within foo, it is perfectly accessible to requests made to /foo.

What you need to take into account is that these functions are just callbacks express keeps references to, and it invokes them when it arrives at a particular one of them. They are processed in order, until you call next, which waterfalls into the next middleware. If you don't call next, you should be ending the response.

I'm not sure exactly how you think the lifecycle works, but it probably doesn't work the way you think. Node just sits around and waits for requests to come in, and then processes them one at a time, it never stops, and it never really "reloads" anything.

like image 121
bevacqua Avatar answered Sep 20 '22 02:09

bevacqua