Dynamic routing performance in node.js with express

I have to create routes of the type /:username in an express application. I can think of two ways for this and i wonder which is more performance optimized. The first is to dynamic serve the route with a call to the db and if the username exists to serve the profile needed. The second one is to create a function, so that when a user is created his profile url is hardcoded into the app and then removed when a user is deleted. This way there won't be calls to the db whenever a url of this type is requested. So the question is what would be the performance problems in the second case, if any and what are the advantages and disadvantages on each case, mainly performance-wise?

Do the first one. I cannot speak towards performance (however I feel that the first will be faster in the long-run), however what happens if your application were to (not saying this would happen) be as popular as Facebook and you then have 1 000 000 000 routes in your express application? Even trying to start your app would get ridiculous.

Databases can handle this, and if you're really worried about it you could keep a cache of the usernames that have already been checked. Add them when you first check it, and delete them if the username is deleted.

It also occurs to me now; will you not have to perform pretty much the same query to get the information to populate the profile anyways? If you are suggesting to create static pages for each profile when the account is created, don't do this. This is what databases were designed for, so it is perfectly safe to use them in this way.

I simply use /:username and i have it below my other routes, so it doesnt' supersede other pages like /login

If there is no user for that username, then I redirect them to the homepage.

Using mongoose, you can do something like this:

//app.js
app.get('/:username', routes.profile.get);

//route handler
User.findOne({ username: req.params.username}, function(err, owner){
  if ( !owner ) {
      req.flash('error', 'Woops, looks like that account doesn\'t exist.');
      res.redirect('/');
  }

  //do something with owner
});