Skip to content

Concurrency runtime for JavaScript

Move JavaScript
off the main thread.

npm install knittingNode · Deno · Bun
server.ts
import { serve } from "@hono/node-server";
import { Hono } from "hono";
import { createPool, isMain } from "knitting";

export const double = (n: number) => n * 2;

if (isMain) {
  const pool = createPool({
    threads: 1,
  })({ double });

  const app = new Hono().get("/", async (c) =>
    c.json({ result: await pool.call.double(21) })
  );
  serve({ fetch: app.fetch, port: 3000 });
}
❯ bun run server.ts
{ result: 42 }
host bun · main thread
await pool.call.double(21)

same process, shared memory

Call your functions in a thread or a separate process.
No runtime dependencies
Typed calls
Threads or processes
Apache-2.0

In a busy server

Keep fast routes fast.

Move React rendering and JWT signing to one worker. Keep lightweight routes like /ping on the main thread.

Hono · 1 worker · 15-second runs. Results vary by workload.

Request flow

/ping response time p99

16.93 msBefore
2.31 msWith one worker
6,000 req/s · CPU/request: 180 → 183 µs.
Animation is illustrative.

Work stealing

Finish a task. Pick up another.

12 waiting
Worker 1Ready
Queue10 waiting
#01#03#05#06#07#08#09#10#11#12
—
0 done
Worker 2Ready
Queue2 waiting
#02#04
—
0 done
0 completed
Scheduling

Quick start

Run a function in a worker.

main.tsTypeScript
import { serve } from "@hono/node-server";
import { brotliCompressSync } from "node:zlib";
import { Hono } from "hono";
import { createPool, isMain } from "knitting";
export const compress = (html: string) =>
brotliCompressSync(html).byteLength;
if (isMain) {
const pool = createPool({ threads: 1 })({ compress });
const app = new Hono().get("/", async (c) => {
const html = c.req.query("html") ?? "<h1>Hello from Hono</h1>";
return c.json({ compressedBytes: await pool.call.compress(html) });
});
serve({ fetch: app.fetch, port: 3000 });
}

A runtime around your workers.

Built inManual or limitedNot included
Node.js worker threads, Piscina, and Knitting capabilities
FeatureNode.js workersPiscinaKnitting
Task APIManual or limited: Messages + handlersBuilt in: run(task)Built in: Typed function calls
Worker poolsManual or limited: Build your ownBuilt in: Managed poolBuilt in: createPool()
SchedulingManual or limited: Your queueBuilt in: Host task queueBuilt in: Native stealing¹
Task cancellationManual or limited: Custom protocolBuilt in: AbortSignal; ends workerBuilt in: Cooperative shared signal
Soft task timeoutsManual or limited: Your timerManual or limited: Your timerBuilt in: timeout option⁴
Hard wall-clock deadlinesManual or limited: Timer + terminateManual or limited: Timed AbortSignalBuilt in: hardTimeoutMs³
Shared-memory task transportManual or limited: Shared buffersManual or limited: Atomics + messagesBuilt in: Shared mailboxes
Process workersNot included: Separate API²Not included: Threads onlyBuilt in: Same pool API
Runtime supportManual or limited: Node.jsManual or limited: Node.jsBuilt in: Node · Bun · Deno

Examples

Rendering, hashing, and compression.

See how it’s tested.

Tests cover shared memory, cancellation, timeouts, and worker failures.

Pick one slow function.