The JS runtimes
Published in

The JS runtimes

The (hidden) cost of using framework: Oak vs Native HTTP servers in Deno

In one of the previous article, we’ve taken a look at the (hidden) cost of using framework (express as an example) in Node.js. In another previous article, we’ve taken a look at the (hidden) cost of using framework (express as an example) in Bun. In this article, we’ll check the same for Deno. In Deno world, Oak is more popular than express. For a long time, Oak was the only option to use. Only recently, Deno has added support for express through NPM compatibility. We’ll take a look at express in a following article.

The web frameworks were invented / developed to make our lives easier. In Deno world, one of the most popular web framework is oak. The oak framework makes our life easier. It has middlewares, router, automatic request/response body processing, etc. But is there a cost to pay for all the features that we may or may not be using at all?

In this article, we’ll compare a hello world HTTP server in native Deno with Oak. Let’s find out if we pay a performance cost for using a framework. If we do pay a cost, how much it is? Oak is used as an example because it is very popular.

The code

First, let’s look at a very simple hello world app written using oak (the code is taken from oak home page):

import {
Application,
FlashServer,
hasFlash,
Router,
} from "https://deno.land/x/oak/mod.ts";

const appOptions = hasFlash() ? { serverConstructor: FlashServer } : undefined;

const app = new Application(appOptions);

const router = new Router();
router.get("/", (ctx) => ctx.response.body = "Hello world");

app.use(router.routes());
app.use(router.allowedMethods());

await app.listen({ port: 3000 });

As Deno has recently introduced a fast HTTP server, Oak needs explicit configuration to use it. In the above code, we’re using the flash server with Oak.

Now, let’s write a very similar app in native Deno using Deno.serve API. We’d need to develop a minimal router that can check for request method and path. Also, there needs to be top level error handling that sends a 500 response if business logic encounters any kind of unhandled error. Here is the code of such an app in native Deno:

Deno.serve((req) => {
try {
if (req.method !== "GET") {
return new Response(null, { status: 405 });
}
const pathName = new URL(req.url).pathname;
if (pathName !== "/") {
return new Response(null, { status: 404 });
}
return new Response("Hello world");
} catch (e) {
return new Response(null, { status: 500 });
}
}, {
port: 3000,
});

The environment

The performance test is executed in the following environment:

  • MacBook M1
  • 16G RAM
  • Deno v1.27

The tester is a very minimal C++ app that uses high performant and stable libcurl to make HTTP requests.

The results

Let’s run some tests and find out the hidden cost. We’ll run 1M requests for different concurrency levels. We’ll run for 1, 50, 100, 200 concurrent connections.

First, let’s see the comparison for 1 concurrent connection (a total of 1M requests):

Of course, we can’t expect Oak to work faster than native Deno. The reason being that Oak is running over the native Deno server. The question is: how much degradation does Oak causes? For the above test, i.e. sending sequential requests, Oak’s RPS is pretty close to native server (18k vs 24k). Oak uses more CPU, while the memory usage is about the same.

Next, let’s look at the results for 50 concurrent connections (a total of 1M requests:

For 50 concurrent connections, the native server takes the lead ahead. The RPS is 77% more than Oak. Oak’s CPU usage stays more, while memory usage stays the same.

Next, let’s look at the results for 100 concurrent connections (a total of 1M requests:

Lastly, let’s look at the results for 200 concurrent connections (a total of 1M requests:

For 100 and 200 concurrent connections, the native server stays about 70% faster than Oak. Oak’s CPU usage is almost the double of native, while memory usage is comparable.

With rich functionality, of course there is a price to pay. Nothing is free, after all. However, the price turned out to be pretty okay.

Thanks for reading! More articles on similar topics can be seen in the magazine: The JS runtimes.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store