Hono vs Fastify
Hono (Edge-first, Cloudflare Workers) vs. Fastify (Node.js-Performance) — Vergleich moderner JS-Web-Frameworks. Performance, Edge-Kompatibilität, Ökosystem.
Der Standard im Node-Ökosystem: flexibel, ausgereift, riesiger Middleware-Pool
Schema-basiert, performanceorientiert: jede Route ein Vertrag
Fastifys offizieller Benchmark (mit dem Hinweis "illustrative") liegt beim Durchsatz 1,6× und bei der Latenz 1,7× vorn, gestützt durch Schema-Serialisierung und das offizielle OpenAPI-Plugin. Express 5s Gewinn ist das automatische Fehlerfangen in async Handlern. Bei GitHub liegt Express klar vorn (69.478 vs. 37.195 Stars). Neuer, performance-kritischer Service → Fastify; bestehende Express-Codebasis + vertrautes Team → bei Express 5 bleiben, es gibt kein offizielles Migrationstool.
| Kategorie | Express 5 | Fastify |
|---|---|---|
| Performance | 6/10 | 9/10 |
| Erlernbarkeit | 9/10 | 7/10 |
| Ökosystem | 9/10 | 7/10 |
| Community | 10/10 | 7/10 |
| Arbeitsmarkt | 8/10 | 6/10 |
| Zukunftssicherheit | 7/10 | 8/10 |
// Express 5 - Async-Route-Handler + automatisches Fehlerfangen
import express from 'express';
const app = express();
app.use(express.json());
// Einfache Fehlerklasse — für eine aussagekräftige Meldung an der API-Grenze
class NotFoundError extends Error {
status = 404;
}
async function getUser(id) {
const user = await db.users.findById(id);
if (!user) throw new NotFoundError('User not found');
return user;
}
// Express 5: Wenn der async Handler rejected oder wirft, wird next(err) automatisch aufgerufen.
// Vergiss nicht, das Promise zu returnen — sonst kann die Rejection unhandled bleiben.
app.get('/api/users/:id', async (req, res) => {
const user = await getUser(req.params.id);
res.json(user);
});
// Zentrale Fehler-Middleware — wird ans Ende des globalen Stacks angehängt
app.use((err, req, res, next) => {
const status = err.status ?? 500;
res.status(status).json({ error: err.message });
});
app.listen(3000, () => {
console.log('Express server listening on port 3000');
});// Fastify 5 - Schema-Validierung + automatische Serialisierung
import Fastify from 'fastify';
const fastify = Fastify({ logger: true });
// Response-Schema: fast-json-stringify kompiliert aus diesem Schema eine
// Serialisierungsfunktion — schneller als das universelle JSON.stringify.
const userSchema = {
type: 'object',
properties: {
id: { type: 'string' },
name: { type: 'string' },
email: { type: 'string' },
},
required: ['id', 'name', 'email'],
};
const getUserOpts = {
schema: {
params: {
type: 'object',
properties: { id: { type: 'string' } },
required: ['id'],
},
response: {
200: userSchema,
},
},
};
// Encapsulation: In diesem Plugin registrierte Hooks/Decorators
// sickern nur in die Routes innerhalb dieses Contexts durch.
fastify.register(async function userRoutes(instance) {
instance.get('/api/users/:id', getUserOpts, async (request, reply) => {
const user = await db.users.findById(request.params.id);
if (!user) {
return reply.code(404).send({ error: 'User not found' });
}
request.log.info({ userId: user.id }, 'user fetched');
return user;
});
});
fastify.setErrorHandler((error, request, reply) => {
reply.status(error.statusCode ?? 500).send({ error: error.message });
});
await fastify.listen({ port: 3000 });Fastifys offizieller Benchmark (mit dem Hinweis "illustrative") liegt beim Durchsatz 1,6× und bei der Latenz 1,7× vorn, gestützt durch Schema-Serialisierung und das offizielle OpenAPI-Plugin. Express 5s Gewinn ist das automatische Fehlerfangen in async Handlern. Bei GitHub liegt Express klar vorn (69.478 vs. 37.195 Stars). Neuer, performance-kritischer Service → Fastify; bestehende Express-Codebasis + vertrautes Team → bei Express 5 bleiben, es gibt kein offizielles Migrationstool.
Kostenlose Beratung erhaltenBaust du einen neuen, performance-kritischen Service mit Schema-Validierung, dann Fastify; hast du bereits in Express investiert und ein vertrautes Team, dann Express 5. Beide werden aktiv gepflegt — im npm-Registry steht express@latest bei 5.2.1, fastify@latest bei 5.12.5 (Fastify v6 befindet sich im Alpha-Stadium, stabil ist aber weiterhin 5.x) — entscheide also nicht mit der Begründung "Express ist tot". Brauchst du breite Unterstützung durch Drittanbieter-Middleware, verschiebt sich die Waage zusätzlich Richtung Express.