Mengoptimasi performa Hono di edge: benchmarking dengan autocannon, tiny preset untuk bundle < 12KB, response caching dengan Cache-Control, profiling di Cloudflare Workers, dan memory management untuk stateless workers.

Setelah di episode 15 kita meng-setup OpenAPI documentation, pada episode ini kita akan memahami cara mengoptimasi performa Hono di edge — krusial untuk Cloudflare Workers di mana CPU time dibatasi per request.
bun add -g autocannonautocannon -c 100 -d 10 http://localhost:8787Target untuk simple route di Cloudflare Workers: ~840k req/s (Mei 2026).
autocannon -c 100 -d 10 -m POST \
-H "Content-Type=application/json" \
-b '{"name":"test","email":"test@example.com"}' \
http://localhost:8787/usersUntuk edge deployment, bundle size sangat berpengaruh. Hono menyediakan tiny preset yang mengurangi features dan bundle size:
import { Hono } from 'hono/tiny'
const app = new Hono()
// < 12KB total bundleTiny preset menghilangkan beberapa fitur yang tidak selalu dibutuhkan — cukup untuk simple API.
app.get('/data', (c) => {
c.header('Cache-Control', 'public, max-age=3600')
return c.json({ data: 'expensive computation' })
})app.get('/api/public-data', (c) => {
c.header('Cache-Control', 'public, max-age=300, s-maxage=3600')
c.header('CDN-Cache-Control', 'max-age=3600')
return c.json({ data: 'cached data' })
})s-maxage berlaku untuk CDN (Cloudflare), max-age untuk browser.
const staticData = { version: '1.0', features: ['fast', 'tiny', 'edge'] }
app.get('/config', (c) => {
c.header('Cache-Control', 'public, max-age=86400')
return c.json(staticData)
})Cloudflare dashboard menyediakan metrics:
Cloudflare Workers memiliki CPU time limit per request. Monitor di dashboard:
app.get('/heavy', async (c) => {
const start = performance.now()
const result = await heavyComputation()
const duration = performance.now() - start
console.log(`CPU time: ${duration}ms`)
return c.json(result)
})Cloudflare Workers bersifat stateless — hindari global state yang bertambah:
// BAD: global state bertambah
const cache = new Map()
// GOOD: gunakan Cloudflare KV untuk persistent state
app.get('/data', async (c) => {
const cached = await c.env.MY_KV.get('data')
if (cached) return c.json(JSON.parse(cached))
const data = await fetchData()
await c.env.MY_KV.put('data', JSON.stringify(data), { expirationTtl: 3600 })
return c.json(data)
})// Durable Objects menyimpan state yang persistent
// dan bisa diakses dari multiple requestsWarning
Cloudflare Workers memiliki limit: 10ms CPU time per request (free tier) atau 30ms (paid). Pastikan tidak ada operasi blocking yang melebihi limit ini.
Pada episode 16 ini, kalian telah memahami cara mengoptimasi performa Hono di edge.
Inti yang harus dibawa pulang:
Di episode 17 selanjutnya kita akan memahami testing dengan Bun test dan Vitest — unit testing routes, integration testing, E2E testing, dan coverage. Sampai jumpa di episode 17!