A quick guide to select a router for a node.js application

Amit Kumar Gupta
3 min readJan 7, 2019

You already have.

Does your framework support custom external routers? If no, don’t read further.

express.js, restana and many other frameworks allow you to use custom routers. Eg

const anumargak = require('anumargak')
const service = require('restana')({
routerFactory: (options) => {
return anumargak(options)
}
})

service.get("/this/is/static", function(req, res){
res.send("Hello");
})

service.get("/this/is/:dynamic", function(req, res){
res.send("Hello");
})

service.start(3001).then((server) => {
console.log("server has been started on port 3001")
});

But muneem, fastify, restify and some more don’t let you select other routers.

Router benchmark suggests that find-my-way is the fastest router. But there are some PRs which are not merged yet. And there can be many programmers who are not even aware with this repo to submit their routers.

Anumargak vs Find-my-way

In my previous article, I’ve talked about the thrilling speed of anumargak But the time has been changed, new features have been added. Hence we need to compare them again.

As we can see in above chart, there are some areas where anumargak is faster and some when find-my-way have found it’s way to go ahead. And if we disable the feature of one-to-multiple mapping in anumargak, above chart will look like this;

So how will you decide which one suits to your project?

1. Does your application has more static and enumerated routes than the routes with dynamic path and wildcard?

/this/is/static/
/this/is/:enumerated(one|two|three)
/this/is/:dynamic([a-z]+)
/docs/*

A dynamic route can have path parameters with unpredicted values. A route with wildcard can be used to handover all the underlying routes to particular handler. Think twice, your application may not need routes with wildcard. they can be routed at ngnix or api-gateway level.

However. If your application has static and enumerated routes more than other then anumargak can be a better choice.

It’s not about number of routes but the % of traffic these routes receive.

2. Do you have similar routes?

/accept/:age([0–9]{2})
/accept/:name([a-z])

If your application can have the routes which may look similar but they’re not then anumargak is the only choice. However, if you’re not good in regex, you may end up creating multiple routes accepting the same type of values. So be careful.

Anumargak also supports a feature to register regex separately so your code become more redable

/send/to/:phone(:phone:)
/authenticate/:token(:alphanum:)

3. Do you process query or fragment string a lot?

/this/is/a/route?ref=medium.com

You’ll notice in chart that find-my-way process a route faster than anumargak if it has query or fragment string. However, anumargak is slower because it breaks the second part and set it to request so that a framework can use it.

If you don’t need the incoming query or fragement (hash) string then find-my-way will be a good choice.

Don’t waste your time

If you compare the routers with each other, you may be confused. But if you use them in a framework and then compare, you’ll get almost the same speed.

Running restana with find-my-way: 21208.09 [#/sec]
Running restana with anumargak: 22037.26 [#/sec]
Running muneem: 26716.78 [#/sec] (mean)

Express.js is approx 1.5 times slower than muneem, restana, or fastify. I’ve tried to run it with find-my-way and anumargak both. But it’s performance was improved by just 1–2%.

Both routers are at least 10 times faster than any node.js framework. So no matter which router you choose, it should not impact the performance of your framework. You actually don’t need this much speed. You should focus on the features you need.

So as the first line of this article says

You don’t need to change the router you’re currently using.

--

--