In this guide, we compare express.js, one of the most commonly used frameworks for building Web applications with Node.js, with tinyHTTP, one of its most popular alternatives.

Here’s what we have to say.

  • What is TinyHTTP?
  • What is Express.js?
  • Tinyhttp and Express. Js. A basic comparison
  • Performance benchmarking
  • Practical application of Tinyhttp and express.js. A simple example

What is TinyHTTP?

Tinyhttp is a modern networking framework similar to Express.js written in TypeScript. Compared to express.js, it uses very few dependencies, which makes it super fast.

Tinyhttp provides routing, REQ/RES extensions, and more, while relying on just six modules, four of which are tinyHTTP’s own.

All middleware created for Express.js works well with TinyHTTP. Take a look at this tinyHTTP + Express/GraphQL integration example to see how to use express.js middleware with TinyHTTP.

Tinyhttp also supports async/await for routing, which does not exist in express.js. Tinyhttp has the appropriate types inside and a function inspector to handle asynchronous functions. In other words, both synchronization and asynchrony work fine in TinyHTTP. Take a look at these async and MongoDB examples to see how to make async handlers.

Here’s a short list of tinyHTTP’s most important features, according to the official documentation.

  • 2.5 times faster than Express
  • Full Express middleware support
  • Support for asynchronous middleware
  • Native ESM and CommonJS support
  • There are no legacy dependencies, just JavaScript itself
  • Out of the box type
  • There is no built-in middleware support

What is Express.js?

Express.js is a flexible Node.js framework that provides a powerful set of capabilities for Web and mobile applications. Creating powerful apis is easy because express.js comes with a lot of middleware and other built-in support.

Express.js does not come with a database, it is done by a third-party module, which allows you to connect to almost any database. Express.js supports numerous template engines, with (Path, locals, callback) signatures.

The framework is built as a minimal and flexible Node.js web application framework that provides a powerful set of capabilities for building single-page, multi-page, and hybrid web applications.

Some features of Express.js.

  • Open Source Community Support
  • Rapid application development
  • Easy to learn
  • Support for template engines
  • The I/O processing
  • Built-in middleware support

Tinyhttp vs. Express.js: A basic comparison

For a high-level comparison of TinyHTTP and Express V4, let’s look at the minimum node.js version supported, ECMAScript version, test coverage, and more.

standard tinyhttp Express v4
The lowest supported version of Node.js 12.4.0 0.10.0
Lowest supported version of ECMAScript ES2019 ES5 (?)
req /resextension
Test coverage 92% 100%
Compile to local ESM
Support the TypeScript
Package size (core only 35.2 kB 208 kB
Built-in middleware

A few notes on the table above.

  • The minimum Node.js version required for TinyHTTP to work is 12.4.0, and in Express V4 we can even start with Node.js 0.10.0.
  • The lowest supported version of TinyHTTP is ES2019 (ES10), and with Express V4, we need ES5.
  • reqIs an object that contains information about the HTTP request that raised the event. In thereqThe response you useresTo send back the desired HTTP response. Both TinyHTTP and Express V4 are supportedreq/resExtension.
  • The code base test coverage was 92% for TinyHTTP and 100% for Express.js V4.
  • The ESM (EcmaScript Modules) specification, introduced with the ES6 (or ES2015) specification, describes how to import and export Modules in JavaScript. We can use ESM in TinyHTTP, but we need external support, such as Babel, for express.js.
  • Unlike express.js, Tinyhttp comes with TypeScript support.
  • Tinyhttp’s core package size is 35.2kB, while express.js is 208kB.
  • Tinyhttp has no built-in middleware. Express.js has built-in middleware.

Performance benchmarking

With the basics out of the way, let’s look at a performance report using the Fastify benchmark tool. Here, we use parameters such as REq/S, Transfer/S, and Latency to measure the performance of the two frameworks.

Here are some instructions on the hardware, systems, and conditions used for this benchmark.

  • hardware.
    • Xiaomi Pro 2019 (laptop
    • The CPU. Intel Core I7-8550U
    • Memory: 16 gb
  • System.
    • Kernel: 5.7.19-2
    • Node. 15.5
  • conditions.
    • 100 the connection
    • Article 10 the pipeline
    • 40s duration
The framework req/s Transmission/SEC. delay
@tinyhttp/app (w/o exts) 24575 3.81 MB 3.37 milliseconds
@tinyhttp/app (esm) 22820 3.54 MB 4.04 ms
@tinyhttp/app (cjs) 22637 3.51 MB 4.08 ms
[email protected] 12986 2 MB 7.11 ms

Req /s is the number of requests per second. Latency is the delay between the user’s action and the network application’s response to that action, also known as the total round-trip time.

From the table above, we can see that TinyHTTP is able to do more transfers per second with lower latency than express.js V4

Note: Benchmarks are not completely accurate and may vary from run to run and from machine to machine. The key is to compare proportions rather than absolute values.

To summarize our performance benchmark, TinyHTTP (without additional REq/RES extensions) is ~1.9 times faster than Express.js.

Tinyhttp and express.js are running. A simple example

Now it’s time to create a simple application example so we can see tinyhttp and Express.js working side by side.

You can install TinyHTTP and Express using any package manager. I’ll use NPM to demonstrate.

Tinyhttp is installed.

npm i @tinyhttp/app

Copy the code

Install Express. Js.

npm install express

Copy the code

‘Hello World

Express.js and TinyHTTP applications are very similar in structure. If you know express.js, you also know tinyhttp.

Tinyhttp.

import { App } from '@tinyhttp/app' const app = new App() const PORT = 3000 app .get('/', (_, res) => void res.send('<h1>Hello World</h1>')) .listen(PORT, () => console.log(`Started on http://localhost:${PORT}! `))Copy the code

Express. Js.

const express = require('express');
const app = express();

app.get('/', (req, res) => {
  res.send('Hello world');
});

app.listen(3000, () => console.log('listening on port 3000'));

Copy the code

You may have noticed that instead of using require, we used an ESM import. Tinyhttp is designed to work with Native ESMs. Unlike Express, TinyHTTP is compiled as a modular system for ESM and CommonJS. At the same time, it’s still, somehow, much smaller than Express.js.

You can use it to use import/export syntax in Node.js. To set up a Node ESM package, place “type”: “module” in package.json, like this.

{
  "type": "module"
}

Copy the code

Another option is to use the.mjs extension. This way, you don’t need to put the “type” field in package.json. For more information, see the ECMAScript Modules Node.js documentation.

Because the most popular express.js middleware also uses outdated modules, TinyHTTP provides a set of rewrites/rewrites of popular items such as Logger, Session, etc.

routing

Now let’s look at how to handle some basic routing in TinyHTTP and express.js. Express.js includes many helper functions in their REq and RES objects. Tinyhttp use such as res. Send, res. Res., download, redirect method to fully implement the Express, js APIs.

Tinyhttp.

import { App } from '@tinyhttp/app' import { once } from 'events' const app = new App() const PORT = 3000 app.get('/', (req, res) => { res.send('Sent a GET! ') }) app.post('/', async (req, res) => { // Nothing complex here, we just listen to 'data' event and return the data as a promise to a `data` variable const data = await once(req, 'data').then(d => d.toString()) // And then we send it res.end(`Sent some data: ${data}`) }) app.listen(PORT, () => console.log(`Started on http://localhost:${PORT}! `))Copy the code

Express.js

var express = require('express'); var app = express(); app.get('/', function(req, res){ res.send("Send a GET!" ); }); app.post('/', function(req, res){ res.send("hello'! \n"); }); app.listen(3000);Copy the code

conclusion

Tinyhttp is fast and light, and you can start using it in backend applications today. The TinyHTTP repository contains many examples, including the integration of MongoDB and GraphQL. I would recommend tinyHTTP when you need to get started quickly with minimal code.

That said, Express.js remains popular and will continue to be used in major projects and industries due to its broad open source community support and easy learning curve.

Tinyhttp vs. Express.js. Comparing the Node.js framework first appeared on the LogRocket blog.